PDA

View Full Version : Changing MainWindow UI from another QThread.



EdgeLuxe
4th September 2010, 11:37
Hello, I was writing my application and at some point I came in need to increment progress bar from another thread and do some UI changes. I have tried these:
1. Passing UI to thread, and changing it directly, but program came in much errors (QCoreApplication ASSERT failure if I remember correctly).
2. Emitting signals to MainWindow. Seems to work correctly, but only when qt::queuedconnection was set. If I set it to direct connection program throws the same ASSERT failure in debug mode. I am not satisfied with queued connection, because I want to increment progressbar`s value immediately, not when thread finishes.
How could I do this? Thanks in advance.

tbscope
4th September 2010, 15:35
Hello, I was writing my application and at some point I came in need to increment progress bar from another thread and do some UI changes. I have tried these:
1. Passing UI to thread, and changing it directly, but program came in much errors (QCoreApplication ASSERT failure if I remember correctly).

Please read the documentation. It's stated on multiple pages that ALL of the user interface needs to be in the main thread.
You can NOT do user interface related things in a separate thread.

This does NOT mean you can't make your program multithreaded though.


2. Emitting signals to MainWindow. Seems to work correctly, but only when qt::queuedconnection was set. If I set it to direct connection program throws the same ASSERT failure in debug mode. I am not satisfied with queued connection, because I want to increment progressbar`s value immediately, not when thread finishes.

Connections across threads NEED to be queued. Signals and slots are handled in the event loop of the object. If an object lives in a different thread, it has a different event loop and you need to wait till the receiving object is in its own event loop. Therefor you need a queued connection.

It's not true that you need to wait for a thread to finish before you can send something.
But if this is all a little bit too difficult, I suggest you take a look at QtConcurrent and use a Future watcher.