Farzad
Farzad

Reputation: 1939

Force Qt custom signal to be processed immediately?

I have a question about Qt and its signals/slots mechanism.
I have created a custom widget and in that I have created a custom SIGNAL (ShowMessage). This signal is connected to a SLOT, which displays the message (along with the specified timeout) in my main window's status bar.

Now, I have an operation in my class that takes a long time to execute, and it's blocking the UI. I was hoping to emit my signal before starting the operation and when it's finished, emit it again to update the status bar; something like this:

emit ShowMessage(message, timeout);
// Do the long operation
emit ShowMessage(newMessage, timeout);

But my problem is that it seems that Qt waits until the whole operation is finished, and only updates the status bar with newMessage.
Is there a way to somehow "force" immediate processing of my signal, because if I want to resort to threads, then my life will get much more complicated!

Upvotes: 2

Views: 2130

Answers (1)

WhiZTiM
WhiZTiM

Reputation: 21576

Is there a way to somehow "force" immediate processing of my signal

Yes, there is. :-). After you show your first message, call QCoreApplication::processEvents(). This forces all pending events to be processed at the point of call. For example,

emit ShowMessage(message, timeout);
QCoreApplication::processEvents();
// Do the long operation
emit ShowMessage(newMessage, timeout);

Upvotes: 5

Related Questions