<?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[C++, emit signal from custom eventfilter failed]]></title><description><![CDATA[<p dir="auto">I have a question about qt signal/slot and custom eventfilter.<br />
usually, the default eventfilter is looks like "xxx-&gt;installEventFilter(this)".<br />
I create a new class about custom eventfilter. It works well.<br />
but, when I want emit a signal from it, it failed,  signal/slot not work.</p>
<p dir="auto">bellow is some main codes about this:</p>
<h2>PlotsView.cpp</h2>
<pre><code>void PlotsView::createPlot()
{
         std::map&lt;std::string, std::string&gt; param_map =
         {
             {"tickerstr", 1234}
         };

         QHBoxLayout *candleplotlay = new QHBoxLayout();

         PlotCreatorDF  customcandle;
         QCustomPlot *candleplot;
         candleplot = customcandle.setupCandlestick(param_map);

         QObject::connect(&amp;customcandle, SIGNAL(atesty(int)), this, SLOT(atestyy(int)));

         plotcontainerlay-&gt;addWidget(candleplot);

}


void PlotsView::atestyy(int x)
{
    qDebug() &lt;&lt; "[CandleNewSecurity_List&gt;&gt;atestyy] x: " &lt;&lt; x;
}


</code></pre>
<h2>PlotCreatorDF.h</h2>
<pre><code>
class PlotCreatorDF : public QWidget
{
    Q_OBJECT
    public:
        explicit PlotCreatorDF(QWidget *parent = nullptr);
        ~PlotCreatorDF();

    public slots:
        void atestz(int x);

    signals:
        void atesty(int x);

};

</code></pre>
<h2>PlotCreatorDF.cpp</h2>
<pre><code>    #include "PlotCreatorDF.h"
    #include "qcustomplot.h"
    #include "Plot/ploteventfilter.h"
    
    PlotCreatorDF::PlotCreatorDF(QWidget *parent) : QWidget(parent)
    {
    }
    
    PlotCreatorDF::~PlotCreatorDF()
    {
        qDebug()&lt;&lt;__LINE__&lt;&lt;__FUNCTION__;
    }
    
    
    QCustomPlot* PlotCreatorDF::setupCandlestick(std::map&lt;std::string, std::string&gt; param_map)
    {
    
        QCustomPlot *daily_plot = new QCustomPlot();
        daily_plot-&gt;setObjectName("test");
    
        //:
        PlotEventFilter *customfilter = new PlotEventFilter();
        daily_plot-&gt;installEventFilter(customfilter);
    
    
        connect(customfilter, SIGNAL(atestx(int)), this, SIGNAL(atesty(int)));
        connect(customfilter, SIGNAL(atestx(int)), this, SLOT(atestz(int)));
    
        return daily_plot;
    
    }
    
    void PlotCreatorDF::atestz(int x)
    {
        qDebug() &lt;&lt; "[PlotCreatorDF&gt;&gt;atestz] x: " &lt;&lt; "x";  //
    }
</code></pre>
<h2>customeventfilter.h</h2>
<pre><code>    #ifndef CUSTOMENTFILTER_H
    #define CUSTOMENTFILTER_H
    #include &lt;QWidget&gt;
    
    namespace Ui {
        class CustomEventFilter;
    }
    
    class CustomEventFilter : public QWidget
    {
        Q_OBJECT
        public:
            explicit CustomEventFilter(QWidget *parent = nullptr);
            ~CustomEventFilter();
    
            bool eventFilter(QObject *obj, QEvent *qevt);
        signals:
            void atestx(int x);
    
        private:
    };
    
    #endif // PLOTEVENTFILTER_H
</code></pre>
<h2>customeventfilter.cpp</h2>
<pre><code>    #include "customeventfilter.h"
    CustomEventFilter::CustomEventFilter(QWidget *parent) : QWidget(parent){}

    CustomEventFilter::~CustomEventFilter()
    {
        qDebug()&lt;&lt;__LINE__&lt;&lt;__FUNCTION__;
    }
    bool CustomEventFilter::eventFilter(QObject *obj, QEvent *qevt){
    
        if (qevt-&gt;type() == QEvent::MouseButtonPress) { //MouseButtonPress
            qDebug() &lt;&lt; "[CustomEventFilter&gt;&gt;eventFilter] mouse clicked ... ";
            emit atestx(123);
        }
        return false;
    }
</code></pre>
<p dir="auto">The connection seems failed to create the connection, and the slot function atestz not work.</p>
<p dir="auto">Can anyone help me, why and how, thanks!</p>
<p dir="auto">I have to say all the connections in the code not work.</p>
<p dir="auto">It seems that any signal can't be sent from "CustomEventFilter".</p>
]]></description><link>https://forum.qt.io/topic/139420/c-emit-signal-from-custom-eventfilter-failed</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 05:07:25 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139420.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Sep 2022 11:54:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++, emit signal from custom eventfilter failed on Mon, 19 Sep 2022 13:08:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/xkungfu">@<bdi>xkungfu</bdi></a><br />
Your problem is not a custom event filter and emitting of signals.  That will work.  Your code is faulty.</p>
<p dir="auto">You need to understand object lifetimes and local variables.  Your <code>PlotCreatorDF  customcandle</code> is a local variable.  You use that as the <code>this</code> in your <code>connect()</code>s  But as soon as method <code>PlotsView::createPlot()</code> exits the <code>PlotCreatorDF  customcandle</code> is destroyed.  Its slots cannot be called, so you don't see the signal (correctly emitted) any longer.  This is where your <em>problem lies</em>.</p>
<p dir="auto">Qt is clever enough to see when <code>customcandle</code> goes out of scope and gets destroyed.  It disconnects any signals connected to its slots, so your <code>connect()</code>s get removed.  This is <em>why the slots don't get called but you don't get an error</em>.</p>
<p dir="auto">Change <code>PlotCreatorDF  customcandle</code> over to <code>PlotCreatorDF  *customcandle = new PlotCreatorDF() </code> and your code will work/signals fired/slots called.  Of course you then have to think about when it should now get destroyed, to avoid a leak.</p>
<p dir="auto">You already do this in <code>PlotEventFilter *customfilter = new PlotEventFilter();</code>, and likely that leaks too?</p>
]]></description><link>https://forum.qt.io/post/729299</link><guid isPermaLink="true">https://forum.qt.io/post/729299</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Sep 2022 13:08:00 GMT</pubDate></item><item><title><![CDATA[Reply to C++, emit signal from custom eventfilter failed on Mon, 19 Sep 2022 14:12:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
thank you very much!<br />
without you answer not only I can't understand lifetimes , but also I would never to think about memory.</p>
]]></description><link>https://forum.qt.io/post/729312</link><guid isPermaLink="true">https://forum.qt.io/post/729312</guid><dc:creator><![CDATA[xkungfu]]></dc:creator><pubDate>Mon, 19 Sep 2022 14:12:53 GMT</pubDate></item><item><title><![CDATA[Reply to C++, emit signal from custom eventfilter failed on Mon, 19 Sep 2022 13:08:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/xkungfu">@<bdi>xkungfu</bdi></a><br />
Your problem is not a custom event filter and emitting of signals.  That will work.  Your code is faulty.</p>
<p dir="auto">You need to understand object lifetimes and local variables.  Your <code>PlotCreatorDF  customcandle</code> is a local variable.  You use that as the <code>this</code> in your <code>connect()</code>s  But as soon as method <code>PlotsView::createPlot()</code> exits the <code>PlotCreatorDF  customcandle</code> is destroyed.  Its slots cannot be called, so you don't see the signal (correctly emitted) any longer.  This is where your <em>problem lies</em>.</p>
<p dir="auto">Qt is clever enough to see when <code>customcandle</code> goes out of scope and gets destroyed.  It disconnects any signals connected to its slots, so your <code>connect()</code>s get removed.  This is <em>why the slots don't get called but you don't get an error</em>.</p>
<p dir="auto">Change <code>PlotCreatorDF  customcandle</code> over to <code>PlotCreatorDF  *customcandle = new PlotCreatorDF() </code> and your code will work/signals fired/slots called.  Of course you then have to think about when it should now get destroyed, to avoid a leak.</p>
<p dir="auto">You already do this in <code>PlotEventFilter *customfilter = new PlotEventFilter();</code>, and likely that leaks too?</p>
]]></description><link>https://forum.qt.io/post/729299</link><guid isPermaLink="true">https://forum.qt.io/post/729299</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Sep 2022 13:08:00 GMT</pubDate></item><item><title><![CDATA[Reply to C++, emit signal from custom eventfilter failed on Mon, 19 Sep 2022 12:37:51 GMT]]></title><description><![CDATA[<p dir="auto">I can't understand which } effect the connect.  sorry, I am trying to understanding what you said.<br />
I think you  found the problem correctly. do you mean the "return daily_plot" throw the connections away?<br />
so, how can I emit a signal from my custom eventfileter?</p>
]]></description><link>https://forum.qt.io/post/729293</link><guid isPermaLink="true">https://forum.qt.io/post/729293</guid><dc:creator><![CDATA[xkungfu]]></dc:creator><pubDate>Mon, 19 Sep 2022 12:37:51 GMT</pubDate></item><item><title><![CDATA[Reply to C++, emit signal from custom eventfilter failed on Mon, 19 Sep 2022 12:20:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/xkungfu">@<bdi>xkungfu</bdi></a> said in <a href="/post/729289">C++, emit signal from custom eventfilter failed</a>:</p>
<pre><code>{
    PlotCreatorDF  customcandle;
    ...
    QObject::connect(&amp;customcandle, SIGNAL(atesty(int)), this, SLOT(atestyy(int)));
}
</code></pre>
<p dir="auto">If this is supposed to work (fire signal to hit slot) for whatever you are asking about, it won't.  <code>customcandle</code> goes out of scope at <code>}</code> so it will never emit any signals after that.  Is this relevant to what you see?</p>
<p dir="auto">Since no <code>PlotCreatorDF</code> instance persists, the <code>this</code> in <code>PlotCreatorDF::setupCandlestick()</code></p>
<pre><code>        connect(customfilter, SIGNAL(atestx(int)), this, SIGNAL(atesty(int)));
        connect(customfilter, SIGNAL(atestx(int)), this, SLOT(atestz(int)));
</code></pre>
<p dir="auto">means these slots get removed from being connected.</p>
]]></description><link>https://forum.qt.io/post/729290</link><guid isPermaLink="true">https://forum.qt.io/post/729290</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Sep 2022 12:20:35 GMT</pubDate></item></channel></rss>