Hello.
I have a need to send a custom event from a thread into the application, but I have nothing.
I do so:
1. My custom event:
class CustomEvent
: public QEvent{
public:
CustomEvent ()
...
...
}
class CustomEvent : public QEvent
{
public:
CustomEvent ()
: QEvent((QEvent::Type)(QEvent::User + 1)) {}
...
...
}
To copy to clipboard, switch view to plain text mode
2. My implementation of a class QThread:
void MyThread::run()
{
while (!stopped) {
//Processing.
...
...
//For some conditions, generates an event.
CustomEvent *e = new CustomEvent ();
//Processing.
...
...
}
}
void MyThread::run()
{
while (!stopped) {
//Processing.
...
...
//For some conditions, generates an event.
CustomEvent *e = new CustomEvent ();
QCoreApplication::postEvent(qApp, e);
//Processing.
...
...
}
}
To copy to clipboard, switch view to plain text mode
3. My main (and only) widget application:
void TestWidget
::customEvent(QEvent *e
) {
if (e
->type
() == (QEvent::User + 1)) {
qDebug() << "TestWidget::customEvent() caught";
}
}
void TestWidget::customEvent(QEvent *e)
{
if (e->type() == (QEvent::User + 1)) {
qDebug() << "TestWidget::customEvent() caught";
}
}
To copy to clipboard, switch view to plain text mode
But I have a message "caught" is displayed. What am I doing wrong?
PS: I tried to override the same method "eventFilter()", as well as install "installEventFilter(qApp)" but nothing happens. Help please.
Added after 21 minutes:
Oops, sorry problem solved.
Instead
myWidget->installEventFilter(qApp)
myWidget->installEventFilter(qApp)
To copy to clipboard, switch view to plain text mode
need
qApp->installEventFilter(myWidget)
qApp->installEventFilter(myWidget)
To copy to clipboard, switch view to plain text mode
Bookmarks