View Full Version : Threading Issue

1st August 2007, 14:32

I encountered an error when I tried to execute following line of code.


The error got is "ASSERT failure in QCoreApplication::sentEvent:"Cannot sent events to objects owned by a different thread".

This code works fine with Qt3 and the problem is only with Qt4.The code is ported from Qt3.3.8 to Qt 4.3.0.

I will heartly appreciate any suggestions.


1st August 2007, 14:39
So, you call that from a worker thread, right?
Well, in Qt4 this does not work anymore. No widget can be modified by any thread other than the GUI thread.

Add a signal to the worker thread, with 2 parameter.
Add a slot in the GUI class that owns/has access to the status bar.

When you want to change the status message, just emit that signal:

emit updateStatus(strError,15000);

The slot in the GUI:

void GUIClass::updateSlot(QString msg, int code )

Something like this.

When emitting signals across threads, they are actually posted as events in the destination threads.

So your signal will become an event that will cause the slot in the GUI thread to execute asynchronously.
The point is to let the GUI thread handle modifications in the widgets.
The slot will execute in the GUI thread's context.


3rd August 2007, 12:40
Yes,from worker thread..

Thank you marcel


3rd August 2007, 16:45
Novice question. Should you wrap the slot code that displays the status in a QMutex or is this handled by the "event" model.


4th August 2007, 13:45
Well, not really.
Consider this case: two signals are emitted one after another from the thread.
This means two events being posted in the GUI event queue.

Now, these two events will be handled successively, no matter what, even on multiprocessor architectures. There is no way the event handler will handle two events in the same time.
So, the slot is not reentrant in this case.
First it is executed for one of the events, then for the other one.