PDA

View Full Version : How to send custom events from QThread:: run () to application.



kuzulis
26th April 2011, 11:02
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 ()
: QEvent((QEvent::Type)(QEvent::User + 1)) {}
...
...
}


2. My implementation of a class QThread:


void MyThread::run()
{
while (!stopped) {
//Processing.
...
...

//For some conditions, generates an event.

CustomEvent *e = new CustomEvent ();
QCoreApplication::postEvent(qApp, e);

//Processing.
...
...
}
}


3. My main (and only) widget application:


void TestWidget::customEvent(QEvent *e)
{
if (e->type() == (QEvent::User + 1)) {

qDebug() << "TestWidget::customEvent() caught";
}
}


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)

need


qApp->installEventFilter(myWidget)


:)