<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Best way to connect a grid of buttons to a function which takes the row and column of the button as argument]]></title><description><![CDATA[<p dir="auto">Suppose I have a grid of buttons. When user clicks on any of these buttons I would like to know what button the user clicked (the row and column number for the button) and use this info to do something</p>
<p dir="auto">So I have managed to do this, but would like to see if there are other, perhaps better, ways of achieving this.  Using a widget application this is what I have done so far:</p>
<pre><code>Widget::Widget(QWidget *parent): QWidget(parent)
{
    QGridLayout * grid = new QGridLayout(this);

    for(int i = 0; i &lt; columns; i++)
    {
        for(int j = 0; j &lt; rows; j++)
        {
            QPushButton * button = new QPushButton(this);
            grid-&gt;addWidget(button, j, i);

            // Set size text etc. for each button 

            connect(button, &amp;QPushButton::clicked, [=](){
                func(i, j);     // Call the function which uses i and j here
            });
        }
    }
}

void Widget::func(int i, int j)
{
    // Do stuff with i and j here
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/122789/best-way-to-connect-a-grid-of-buttons-to-a-function-which-takes-the-row-and-column-of-the-button-as-argument</link><generator>RSS for Node</generator><lastBuildDate>Mon, 16 Mar 2026 14:33:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/122789.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 17 Jan 2021 15:43:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Best way to connect a grid of buttons to a function which takes the row and column of the button as argument on Sun, 24 Jan 2021 10:35:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Thanks for your answer. I am just trying to learn QT right now, and this was just a random problem I gave myself :)</p>
]]></description><link>https://forum.qt.io/post/639827</link><guid isPermaLink="true">https://forum.qt.io/post/639827</guid><dc:creator><![CDATA[saa_]]></dc:creator><pubDate>Sun, 24 Jan 2021 10:35:43 GMT</pubDate></item><item><title><![CDATA[Reply to Best way to connect a grid of buttons to a function which takes the row and column of the button as argument on Sun, 17 Jan 2021 19:47:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Hehe, right.<br />
Well if the question was how do i have an obscene amount of pushbuttons I would have<br />
told him to paint them and keep a list of rects and do own hittesting.<br />
10K buttons in a layout would also be heavy when resizing :)</p>
]]></description><link>https://forum.qt.io/post/638443</link><guid isPermaLink="true">https://forum.qt.io/post/638443</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 17 Jan 2021 19:47:35 GMT</pubDate></item><item><title><![CDATA[Reply to Best way to connect a grid of buttons to a function which takes the row and column of the button as argument on Sun, 17 Jan 2021 17:22:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a><br />
If there are 100 rows and 100 columns there will be 10,000 <code>connect()</code>s.  (And 10,000 <code>QPushButton</code>s; but that's not what we've been asked about.)</p>
]]></description><link>https://forum.qt.io/post/638421</link><guid isPermaLink="true">https://forum.qt.io/post/638421</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 17 Jan 2021 17:22:46 GMT</pubDate></item><item><title><![CDATA[Reply to Best way to connect a grid of buttons to a function which takes the row and column of the button as argument on Sun, 17 Jan 2021 16:24:24 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
Using a custom widget to hold the buttons and lambda to capture col, row seems<br />
pretty fine.</p>
<p dir="auto">You could add a new signal</p>
<p dir="auto">void ButtonPressed(int col, int row);</p>
<p dir="auto">To "Widget"<br />
and emit that in the lambda to allow other classes to hook into the clicked signal to do custom processing if<br />
needed.</p>
<p dir="auto">That way you can reuse widget in other cases, where clicking on a button will do something different.</p>
<pre><code>
            connect(button, &amp;QPushButton::clicked, [=](){
                emit ButtonPressed(i,j);
            });
</code></pre>
]]></description><link>https://forum.qt.io/post/638413</link><guid isPermaLink="true">https://forum.qt.io/post/638413</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 17 Jan 2021 16:24:24 GMT</pubDate></item></channel></rss>