Qt Update Ui From Thread
Hey i heard that there is something called qt signals you can use it to change the GUI out side the. Hey i heard that there is something called qt signals you can use it to change the GUI out side the main thread coz when i try to change it. Qt-updating ui by thread. Connection to be Qt::QueuedConnection (which should be already done automatically). Packets uninterrupted and I want to update at. Update GUI from QThread Update GUI from QThread. This topic has been deleted. Only users with topic management privileges can see it. So what you're doing is to schedule the updates in the main thread. This is why when the event loop of the main thread is busy you get the repaints done later. Looks like your connection to Qt Forum was. 'How to use QThread in the right way (Part 1)' Mon, 05 Aug 2013. But when SLOTS and Qt event loop are used in the worker thread, some users do it wrong. So Bradley T. Hughes, one of the Qt core developers. Use worker objects by moving them to the thread is easy to use when event loop exists. Updating a Qt GUI from a boost::thread OK, first things first. After seeing how horrible the previous post looked, I've decided to look into the subject, to improve the readability here. Is this an acceptable/safe way to update GUI from another thread? Is this an acceptable/safe way to update GUI from another thread? This topic has been deleted. Only users with topic management privileges can see it. I understand that it is unsafe to update the Qt GUI from worker threads, and that the main GUI thread should be signaled to.
I need to have a computationally intensive function run in a different thread so that the GUI doesn't freeze or turn grey when it's running.
I followed this example: https://stackoverflow.com/a/16501374/2904614
But the GUI still freezes and turns grey.
MainWindow.cpp
When the user clicks the designated button. Since there may be a lot of files in one directory, the GUI will freeze as QDirIterator goes through all of them. I'm hoping to add a progress bar, that will show the user the program is still functioning.
EDITI would like to have the function MainWindow::getCheckSum() run in a different thread than the GUI. How will I implement this?
Github: https://github.com/Jyang772/PenguSniff
3 Answers
Given you only need to run one function i would recommend QtConcurrent. Extract form the docs:
Running a Function in a Separate Thread

To run a function in another thread, use QtConcurrent::run():
This will run aFunction in a separate thread obtained from the default QThreadPool. You can use the QFuture and QFutureWatcher classes to monitor the status of the function.
Sebastian LangeSebastian LangeThe problem is that you are calling the function of a class in another thread directly:
It causes undefined behavior in your application and makes it crash. The correct way is to emit a signal from your MainWindow which is connected to the getHash() slot in updater:
This will result in a queued connection and runs the slot in the updater thread.
NejatNejatYou call your getHash() computational function (it is computational as per your comment) in GUI thread. Bind it to clicked signal, then it will be invoked in object's thread.
BasilevsBasilevsNot the answer you're looking for? Browse other questions tagged c++multithreadingqt or ask your own question.
i have an multithreaded qt application. when i am doing some processes in mainwindow.cpp, at the same time, i want to update mainwindow.ui from other thread.
i have mythread.h
mythread.cpp
but the problem is that, i cannot reach the ana->ui->horizontalLayout_4->addWidget(label);
how can i do that?
dtech3 Answers
but the problem is that, i cannot reach the ana->ui->horizontalLayout_4->addWidget(label);
Put your UI modifications in a slot in your main window, and connect a thread signal to that slot, chances are it will work. I think only the main thread has access to the UI in Qt. Thus if you want GUI functionality, it must be there, and can be only signaled from other threads.
Qt Update Ui From Threading
OK, here is a simple example. BTW, your scenario doesn't really require to extend QThread - so you are better off not doing it, unless you really have to. That is why in this example I will use a normal QThread with a QObject based worker instead, but the concept is the same if you subclass QThread:
The main UI:
.. and the worker object:
The worker object is created and moved to another thread, then connected to the slot that creates the labels, then its newLabel method is invoked, which is just a wrapper to emit the requestNewLabel signal and pass the path to the image. The signal is then passed from the worker object/thread to the main UI slot along with the image path parameter and a new label is added to the layout.
Since the worker object is created without parent in order to be able to move it to another thread, we also connect the thread destroyed signal to the worker deleteLater() slot.
First and foremost, 'you're doing it wrong'. Normally you want to create a class derived from a QObject and move that class to a new thread object instead of deriving your class from a Qthread
Now to get onto the specifics of your question, you're not able to directly modify the ui elements of your main GUI thread from a separate thread. You have to connect a signal from your 2nd thread to a slot in your main thread. You can pass any data that you need through this signal/slot connection but you're unable to directly modify the ui element (which in all honestly you probably do not want to if you intend to keep the frontend of your app separate from the backend). Checkout Qt's signal and slot documentation for a whole lot more information
how can i do that?
You've already got the answers to what you should be doing, but not a why, so I'm going to add a why.
The reason you don't modify GUI elements from another thread is because GUI elements are usually not thread-safe. This means that if both your main GUI thread and your worker thread update the UI, you cannot be certain of the order of what happened when.
For reading data generally this can sometimes be fine (e.g. checking a condition) but generally you do not want this to be case. For writing data, this is almost always the source of very, very stressful bugs which occur 'at random'.
Kamen rider kabuto god speed love. It also was broadcast in South Korea as Masked Rider Gabuto (가면라이더 가부토 Gamyeon Raideo Gabuto). The first episode aired on January 29, 2006, and with the final episode airing on January 21, 2007, completing the series with 49 episodes. The catchphrase of the series is 'Walking the path of heaven, to rule it all!' The series represents the 35th anniversary of the Kamen Rider Series, as indicated by a notice at the beginning of the pilot episode reading, in Japanese, 'Kamen Rider 35th Anniversary Production.' 天の道を往き、総てを司れ!, Ten no michi o yuki, subete o tsukasadore!
Another answer has remarked on good design principles - not only does constraining your GUI logic to one thread and firing signals to talk to it get rid of your race condition issues, but it also forces you to compartmentalize your code nicely. Presentation logic (the display bit) and data processing logic can then be cleanly separated out, which makes maintaining the two much easier.
At this stage you might think: heck, this threads business is farrrrrr too much work! I'll just avoid that. To see why this is a bad idea, implement a file copy program in a single thread with a simple progress bar telling you how far along the copy is. Run it on a large file. On Windows, after a while, the application will 'go white' (or on XP I think it goes gray) and will be 'not responding'. This is very literally what is happening.
Qt Get Main Thread
GUI applications internally mostly work on the variation of 'one big loop' processing and dispatching messages. Windows, for example, measures response time to those messages. If a message takes too long to get a response, Windows then decides it is dead, and takes over. This is documented in GetMessage().
So whilst it may seem like quite a bit of work, Signals/Slots (an event-driven model) is basically the way to go - another way to think of this is that it is totally acceptable for your threads to generate 'events' for the UI too - such as progress updates and the like.
