installEventFilter(this)". I creat" /> installEventFilter(this)". I creat" /> installEventFilter(this)". I creat"/>

C++ QT, emit signal from custom eventfilter failed

62 Views Asked by At

I have a question about qt signal/slot and custom eventfilter. usually, the default eventfilter is looks like "xxx->installEventFilter(this)". I create a new class about custom eventfilter. It works well. but, when I want emit a signal from it, it failed, signal/slot not work.

bellow is some main codes about this:

PlotsView.cpp

void PlotsView::createPlot()
{
         std::map<std::string, std::string> param_map =
         {
             {"tickerstr", 1234}
         };

         QHBoxLayout *candleplotlay = new QHBoxLayout();

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

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

         plotcontainerlay->addWidget(candleplot);

}


void PlotsView::atestyy(int x)
{
    qDebug() << "[CandleNewSecurity_List>>atestyy] x: " << x;
}


PlotCreatorDF.h


class PlotCreatorDF : public QWidget
{
    Q_OBJECT
    public:
        explicit PlotCreatorDF(QWidget *parent = nullptr);
        ~PlotCreatorDF();

    public slots:
        void atestz(int x);

    signals:
        void atesty(int x);

};

PlotCreatorDF.cpp

    #include "PlotCreatorDF.h"
    #include "qcustomplot.h"
    #include "Plot/ploteventfilter.h"
    
    PlotCreatorDF::PlotCreatorDF(QWidget *parent) : QWidget(parent)
    {
    }
    
    PlotCreatorDF::~PlotCreatorDF()
    {
        qDebug()<<__LINE__<<__FUNCTION__;
    }
    
    
    QCustomPlot* PlotCreatorDF::setupCandlestick(std::map<std::string, std::string> param_map)
    {
    
        QCustomPlot *daily_plot = new QCustomPlot();
        daily_plot->setObjectName("test");
    
        //:
        PlotEventFilter *customfilter = new PlotEventFilter();
        daily_plot->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() << "[PlotCreatorDF>>atestz] x: " << "x";  //
    }

customeventfilter.h

    #ifndef CUSTOMENTFILTER_H
    #define CUSTOMENTFILTER_H
    #include <QWidget>
    
    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

customeventfilter.cpp

    #include "customeventfilter.h"
    CustomEventFilter::CustomEventFilter(QWidget *parent) : QWidget(parent){}

    CustomEventFilter::~CustomEventFilter()
    {
        qDebug()<<__LINE__<<__FUNCTION__;
    }
    bool CustomEventFilter::eventFilter(QObject *obj, QEvent *qevt){
    
        if (qevt->type() == QEvent::MouseButtonPress) { //MouseButtonPress
            qDebug() << "[CustomEventFilter>>eventFilter] mouse clicked ... ";
            emit atestx(123);
        }
        return false;
    }

The connection seems failed to create the connection, and the slot function atestz not work.

Can anyone help me, why and how, thanks!


very sorry, I have add some codes to make the question clearly.

I have to say all the connections in the code not work.

It seems that any signal can't be sent from "CustomEventFilter".

0

There are 0 best solutions below