<?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[How to replicate a click to a parent without subclassing the widget?]]></title><description><![CDATA[<p dir="auto">How do I replicate a click into a <code>QPushButton</code> to her parent (a QFrame) using a lambda connection?</p>
<pre><code>    connect(but, &amp;QPushButton::clicked, this, [this, but]        
    {        
              //but-&gt;parentWidget()-&gt; ?          
    });
</code></pre>
]]></description><link>https://forum.qt.io/topic/141058/how-to-replicate-a-click-to-a-parent-without-subclassing-the-widget</link><generator>RSS for Node</generator><lastBuildDate>Thu, 11 Jun 2026 09:35:58 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/141058.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 27 Nov 2022 14:31:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 22:33:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/roberrt">@<bdi>Roberrt</bdi></a> Depends on what you mean safe. It's not gonna crash or anything like that.  It disconnects everything that's connected to the clicked signal of that particular button. I don't think Qt connects anything to QPushButton's clicked signal internally, so it should be just your connections. This should be fine in simple cases, but in general it's better to be explicit. You never know what connections will be made in future refactoring. You can cause someone to loose a lot of hair trying to figure out why their connections get severed. For some classes Qt also makes some connections internally e.g. when you set a data model on a view. It's easy to break some functionalities doing such bulk disconnections.</p>
<p dir="auto">You can always replace that lambda with a normal function and avoid all that disconnecting trouble using  <code>Qt::UniqueConnection</code>.</p>
]]></description><link>https://forum.qt.io/post/737896</link><guid isPermaLink="true">https://forum.qt.io/post/737896</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 27 Nov 2022 22:33:35 GMT</pubDate></item><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 22:21:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a></p>
<p dir="auto">Thank you, last question, do you know if its "safe" to do such thing:</p>
<pre><code>// Try disconnecting any existing connection even if there'snt any
QObject::disconnect(but, &amp;QPushButton::clicked, this, nullptr);

QMetaObject::Connection con = connect(but, &amp;QPushButton::clicked, this, [this]
{
});
</code></pre>
<p dir="auto">I run it and I didn't see any error or exception, but IDK if its 'safe'</p>
]]></description><link>https://forum.qt.io/post/737895</link><guid isPermaLink="true">https://forum.qt.io/post/737895</guid><dc:creator><![CDATA[Roberrt]]></dc:creator><pubDate>Sun, 27 Nov 2022 22:21:12 GMT</pubDate></item><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 22:17:50 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">Just this or do I need something more?</p>
</blockquote>
<p dir="auto">That's all.</p>
]]></description><link>https://forum.qt.io/post/737894</link><guid isPermaLink="true">https://forum.qt.io/post/737894</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 27 Nov 2022 22:17:50 GMT</pubDate></item><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 22:12:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> said in <a href="/post/737862">How to replicate a click to a parent without subclassing the widget?</a>:</p>
<blockquote>
<p dir="auto">QMetaObject::Connection</p>
</blockquote>
<pre><code>    QMetaObject::Connection con = connect(but, &amp;QPushButton::clicked, this, [this]
    {
    });

    // And when I no longer need it:
    disconnect(con);
</code></pre>
<p dir="auto">Just this or do I need something more?</p>
]]></description><link>https://forum.qt.io/post/737893</link><guid isPermaLink="true">https://forum.qt.io/post/737893</guid><dc:creator><![CDATA[Roberrt]]></dc:creator><pubDate>Sun, 27 Nov 2022 22:12:14 GMT</pubDate></item><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 15:28:41 GMT]]></title><description><![CDATA[<p dir="auto">You'll get two connections and your lambda will be called twice when the event occurs.</p>
<p dir="auto">For regular functions you can use additional parameter to connect: <a href="https://doc.qt.io/qt-6/qt.html#ConnectionType-enum" target="_blank" rel="noopener noreferrer nofollow ugc">Qt::UniqueConnection</a>. That will not create another connection if one with the same parameters already exists, but this will not work with inline lambdas, as those are always unique.</p>
<p dir="auto"><code>connect</code> also returns a <code>QMetaObject::Connection</code> object, which you can use to <a href="https://doc.qt.io/qt-6/qobject.html#disconnect-4" target="_blank" rel="noopener noreferrer nofollow ugc">disconnect</a> it when you no longer need it.</p>
]]></description><link>https://forum.qt.io/post/737862</link><guid isPermaLink="true">https://forum.qt.io/post/737862</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 27 Nov 2022 15:28:41 GMT</pubDate></item><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 15:23:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a> i got what you mean, I'm going with another solution.</p>
<p dir="auto">Just a doubt that comes to my mind, what happens if for <strong>example</strong> I call a connection like:</p>
<pre><code>connect(but, &amp;QPushButton::clicked, this, [this, but]
</code></pre>
<p dir="auto">twice, with repeated parameters in the same button?</p>
<p dir="auto">the previous connection is canceled?<br />
or do I need somehow to 'cancel' the previous connection?</p>
]]></description><link>https://forum.qt.io/post/737861</link><guid isPermaLink="true">https://forum.qt.io/post/737861</guid><dc:creator><![CDATA[Roberrt]]></dc:creator><pubDate>Sun, 27 Nov 2022 15:23:58 GMT</pubDate></item><item><title><![CDATA[Reply to How to replicate a click to a parent without subclassing the widget? on Sun, 27 Nov 2022 15:00:19 GMT]]></title><description><![CDATA[<p dir="auto">A click is a series of mouse press and release events on the same widget. QPushButton emits a <code>clicked</code> signal when it gets a release after a press. A QFrame does not have a notion of a "click". It's not a widget that is meant to be interactive, so "replicate click in QFrame" doesn't have any meaning.</p>
<p dir="auto">You can send press/release events though. See <a href="https://doc.qt.io/qt-6/qcoreapplication.html#sendEvent" target="_blank" rel="noopener noreferrer nofollow ugc">QCoreApplication::sendEvent</a>. You could send a mouse press followed by mouse release events, but you won't get a "clicked" signal from a QFrame. It doesn't have one. If you don't subclass QFrame those events will be just ignored, as that's the default implementation for QFrame.</p>
<p dir="auto">What are you trying to achieve with this? Sounds like a wrong solution to a problem that can be tackled in another way.</p>
]]></description><link>https://forum.qt.io/post/737857</link><guid isPermaLink="true">https://forum.qt.io/post/737857</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 27 Nov 2022 15:00:19 GMT</pubDate></item></channel></rss>