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.
-
@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 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
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); }
-
@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...