[Solved] How do I make a cross-threaded update on a widget?
-
I am writing a server using Qt's networking and GUI interface. I have a read-only QTextEdit widget that serves as a visible log for server events. This includes events regarding new connections which are handled on a separate thread (one for each connection). I want to send information about what is happening on the connection to the log text. (Which means appending text to the log from another thread). It also needs to update an actual log file.
I have done this kind of thing in .NET (cross-thread updates to controls) but I am still relatively new to Qt.
How would I go about doing this? -
I suggest creating a slot that would append the text (in main thread). Then, from your server threads, connect to that slot using Qt::QueuedConnection mode. This way you can be cartain that you UI will not be blocked, and you do not need to worry about mutexes, locking, etc.
-
Thanks. I will try that out but maybe I missed something. What would be the connecting signal that triggered the slot? Perhaps I would need to make my own? If so, I suppose the text needing to be sent would be the argument passed (from the signal to the slot?)
-
Take a look on "Mandelbrot Example":http://qt-project.org/doc/qt-5/qtcore-mandelbrot-example.html
-
[quote author="primem0ver" date="1408252568"]Thanks. I will try that out but maybe I missed something. What would be the connecting signal that triggered the slot? Perhaps I would need to make my own? If so, I suppose the text needing to be sent would be the argument passed (from the signal to the slot?)[/quote]
Yes, you would add a suitable Signal to your Thread/Worker class, e.g. updateAvailable(QString), and a corresponding Slot to the GUI class, e.g. handleUpdate(QString). Then you create a queued connection between the GUI object and each Thread/Worker object. And each time something interesting has happened in the Thread/Worker object it emits the signal.
-
Thanks everyone. I appreciate the help. I will mark this as solved as it is going to take some time before I can compile and see the result.