Send singal in eventFilter
-
Hi.
I want to send signal in eventFilter,but it fail.
Can't send signal in eventFilter?Event.cpp
bool Event::eventFilter(QObject *, QEvent *ev) { if( ev->type() == QEvent::Enter ){ qDebug() << "ENTER!"; emit sigMouse(); return true; } if( ev->type() == QEvent::Leave){ qDebug() << "LEAVE!"; emit sigMouse(); return true; } return false; }
Connect.cpp
Connect::Connect(QObject *parent) : QObject(parent) { connect( &eve, SIGNAL( sigMouse() ), this, SLOT(slotMouse() )); } void Connect::slotMouse() { qDebug() << "HERE"; }
It show "ENTER!" and "LEAVE!", but "HERE” is can't show.
-
@J-Hilk
Sorry, I was wrong.MyWidget.h
namespace Ui { class MyWidget; } class MyWidget: public QWidget { Q_OBJECT public: MyWidget(); private: QVBoxLayout* myMainLayout; Ui::Chart* myWidget; Event event; };
MyWidget.cpp
MyWidget::MyWidget() : QWidget(), myWidget(new Ui::MyWidget) { myWidget->setupUi(this); myMainLayout = new QVBoxLayout; myMainLayout->setMargin(0); myWidget->frame->setLayout(myMainLayout); myWidget->frame->setGeometry(0, 0, 1360, 760); myWidget->rectangle->installEventFilter(&event); }
@Knj-Tkm well, there's your problem,
you have 2 Event instances, one in MyWidget and one in Connect,
that in MyWidget is installed, and emits the debug texts, that one in Connect is not, but you connect to those signals.
You expect citrus smell from an apple :D
Fix that -
Hi.
I want to send signal in eventFilter,but it fail.
Can't send signal in eventFilter?Event.cpp
bool Event::eventFilter(QObject *, QEvent *ev) { if( ev->type() == QEvent::Enter ){ qDebug() << "ENTER!"; emit sigMouse(); return true; } if( ev->type() == QEvent::Leave){ qDebug() << "LEAVE!"; emit sigMouse(); return true; } return false; }
Connect.cpp
Connect::Connect(QObject *parent) : QObject(parent) { connect( &eve, SIGNAL( sigMouse() ), this, SLOT(slotMouse() )); } void Connect::slotMouse() { qDebug() << "HERE"; }
It show "ENTER!" and "LEAVE!", but "HERE” is can't show.
@Knj-Tkm Please check whether your connect() call succeeded (it returns a bool). Even better is to use new Qt5 connect syntax.
Also, it is not clear what eve is and whether it is the one which actually used. -
@Knj-Tkm Please check whether your connect() call succeeded (it returns a bool). Even better is to use new Qt5 connect syntax.
Also, it is not clear what eve is and whether it is the one which actually used. -
@Knj-Tkm said in Send singal in eventFilter:
eve is create an object in Header
And is this eve really the one which is used and gets the events? Do you have another one somewhere?
-
@Knj-Tkm said in Send singal in eventFilter:
eve is create an object in Header
And is this eve really the one which is used and gets the events? Do you have another one somewhere?
-
@Knj-Tkm this
Connect::Connect(QObject *parent) : QObject(parent) { connect( &eve, SIGNAL( sigMouse() ), this, SLOT(slotMouse() )); }
looks like the constructor, where I would have expected to see the event filter applied to the class, where do you apply it ?
-
@Knj-Tkm this
Connect::Connect(QObject *parent) : QObject(parent) { connect( &eve, SIGNAL( sigMouse() ), this, SLOT(slotMouse() )); }
looks like the constructor, where I would have expected to see the event filter applied to the class, where do you apply it ?
-
@J-Hilk
In Header#include <QObject> #include "Event.h" class Connect: public QObject { Q_OBJECT public: Connect(QObject *parent = nullptr); public slots: void slotMouse(void); private: Event eve; };
@Knj-Tkm
so, you're missing the installEventFilter call? -
@Knj-Tkm
so, you're missing the installEventFilter call?@J-Hilk
Sorry, I was wrong.MyWidget.h
namespace Ui { class MyWidget; } class MyWidget: public QWidget { Q_OBJECT public: MyWidget(); private: QVBoxLayout* myMainLayout; Ui::Chart* myWidget; Event event; };
MyWidget.cpp
MyWidget::MyWidget() : QWidget(), myWidget(new Ui::MyWidget) { myWidget->setupUi(this); myMainLayout = new QVBoxLayout; myMainLayout->setMargin(0); myWidget->frame->setLayout(myMainLayout); myWidget->frame->setGeometry(0, 0, 1360, 760); myWidget->rectangle->installEventFilter(&event); }
-
@J-Hilk
Sorry, I was wrong.MyWidget.h
namespace Ui { class MyWidget; } class MyWidget: public QWidget { Q_OBJECT public: MyWidget(); private: QVBoxLayout* myMainLayout; Ui::Chart* myWidget; Event event; };
MyWidget.cpp
MyWidget::MyWidget() : QWidget(), myWidget(new Ui::MyWidget) { myWidget->setupUi(this); myMainLayout = new QVBoxLayout; myMainLayout->setMargin(0); myWidget->frame->setLayout(myMainLayout); myWidget->frame->setGeometry(0, 0, 1360, 760); myWidget->rectangle->installEventFilter(&event); }
@Knj-Tkm well, there's your problem,
you have 2 Event instances, one in MyWidget and one in Connect,
that in MyWidget is installed, and emits the debug texts, that one in Connect is not, but you connect to those signals.
You expect citrus smell from an apple :D
Fix that -
@Knj-Tkm well, there's your problem,
you have 2 Event instances, one in MyWidget and one in Connect,
that in MyWidget is installed, and emits the debug texts, that one in Connect is not, but you connect to those signals.
You expect citrus smell from an apple :D
Fix that -
@Knj-Tkm well, there's your problem,
you have 2 Event instances, one in MyWidget and one in Connect,
that in MyWidget is installed, and emits the debug texts, that one in Connect is not, but you connect to those signals.
You expect citrus smell from an apple :D
Fix that@Knj-Tkm @J-Hilk said in Send singal in eventFilter:
you have 2 Event instances, one in MyWidget and one in Connect
So, my assumption was correct...