Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could connect all of your buttons to a slot with no parameters and then get the position of the sender in this steps:</p> <ol> <li>Cast the sender <code>QObject</code> to a QWidget via <code>qobject_cast</code></li> <li>Retrieve the index of that <code>QWidget</code> using <code>QLayout::indexOf(QWidget *widget)</code></li> <li>Then get the row, column, column span and row span with the <code>QGridLayout::getItemPosition(int index, int *row, int *column, int *rowSpan, int *columnSpan)</code></li> </ol> <p>The example code would look like this:</p> <pre><code>void MyWidgetWithAllLabels::commonSlot() { QWidget *buttonWidget = qobject_cast&lt;QWidget*&gt;(sender()); if (!buttonWidget) return; int indexOfButton = ui-&gt;gridLayout-&gt;indexOf(buttonWidget); int rowOfButton, columnOfButton, rowSpanOfButton, columnSpanOfButton; ui-&gt;gridLayout-&gt;getItemPosition(indexOfButton, &amp;rowOfButton, &amp;columnOfButton, &amp;rowSpanOfButton, &amp;columnSpanOfLabel); // Now you can get a reference to that specific QPushButton QLayoutItem *item = ui-&gt;gridLayout-&gt;itemAtPosition(rowOfButton, columnOfButton); QPushButton *clickedButton = qobject_cast&lt;QPushButton*&gt;(item-&gt;widget()); if (!clickedButton) return; // ... do something with that clickedButton } </code></pre> <p>Referring to the code in your related post, you can connect your buttons to that slot like this:</p> <pre><code>connect( ui-&gt;tile_0_0, SIGNAL(clicked()), this, SLOT(commonSlot())); connect( ui-&gt;tile_0_1, SIGNAL(clicked()), this, SLOT(commonSlot())); // ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload