Delay for QLabel test update
-
Hi
I have a desktop application that processes a TXT file which is located by the user. To inform the user about the process, I use some QLabels and update text of label about the process being held.
@ QLabel informer;
informer.setText("Running...");
while (not_end_of_file) {
process_file();
} @But the QLabel's text is updated after the while() loop finishes.
What should I do to update the text before entering the loop.
Thanks
-
@ QLabel informer;
informer.setText("Running...");
QCoreApplication::processEvents();
while (not_end_of_file) {
process_file();
} @ -
When you call "setText":http://doc.qt.nokia.com/latest/qlabel.html#text-prop changes will redraw in paintEvent so it will be calling in event loop, until while() not ends there is no repainting to label.
As @cincirin wrote you need "processEvents":http://doc.qt.nokia.com/latest/qcoreapplication.html#processEvents before you call while(...)
[EDIT:] mistakes in explanation -
It is a bit saver to change line 3 above into:
@
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
@
That way, you can be sure that user input will not be processed at that point, which is a Good Thing(TM) as your application is probably in an inconsistent state: you do not anticipate such interaction at that point. Otherwise, you might find that a button-click signal could be processed at that moment, for instance. -
[quote author="rokemoon" date="1321441421"]"setText":http://doc.qt.nokia.com/latest/qlabel.html#text-prop is a slot so it will be calling in event loop, until while not ends there is no changing to label.[/quote]
That is wrong:
Slots are not called through the eventloop if the object the slot is in has the affinity of the current thread. Furthermore, slots are just methods, and that is way setText is called here. That completely circumvents the signal-slot mechanism, even if this call would be across threads (in which case, it would be wrong, as you cannot call anything that changes the UI from outside the UI thread, but that is different topic). -
[quote author="Andre" date="1321441817"]
[quote author="rokemoon" date="1321441421"]"setText":http://doc.qt.nokia.com/latest/qlabel.html#text-prop is a slot so it will be calling in event loop, until while not ends there is no changing to label.[/quote]
That is wrong:
Slots are not called through the eventloop if the object the slot is in has the affinity of the current thread. Furthermore, slots are just methods, and that is way setText is called here. That completely circumvents the signal-slot mechanism, even if this call would be across threads (in which case, it would be wrong, as you cannot call anything that changes the UI from outside the UI thread, but that is different topic).[/quote]
Oh my God what I'm telling about o_O , really SORRY. of course not in event loop it's just a method.
That's all, I need the rest and should go home :-( -
A [[doc:QProgressDialog]] seems to be a better solution for this than a QLabel. Have a look at "this older thread":/forums/viewthread/1436, especially at the "snippet here":/forums/viewreply/43702/.
Edit: fixed documentation link from QProcessDialog to QProgressDialog; Andre