Qt update gui from different thread11/7/2022 ![]() ![]() Higher values can be used for custom events. Qt's built-in events have values below 1000. If ((int)event->type() = TransactionStart) Void ImageWindow::customEvent(QCustomEvent *event) The addTransaction() function adds a transaction to the non-GUI thread's transaction queue and disables the Open, Save, and Save As actions while transactions are being processed. Void ImageWindow::addTransaction(Transaction *transact) The flipVertical(), resizeImage(), convertTo32Bit(), convertTo8Bit(), and convertTo1Bit() functions are similar. The flipHorizontally() slot creates a "flip" transaction and registers it using the private function addTransaction(). The thread variable is of type TransactionThread, which we will cover in a moment.ĪddTransaction(new FlipTransaction(Horizontal)) The thread will post progress events to that widget. In the ImageWindow constructor, we set the "target widget" of the non-GUI thread to be the ImageWindow. ![]() ImageWindow::ImageWindow(QWidget *parent, const char *name) ![]() The non-GUI thread has a list of tasks, or "transactions", to accomplish, and sends events to the main window to report progress. This makes a significant difference when processing large images. The application uses one non-GUI thread to perform operations on images without locking the event loop. QT UPDATE GUI FROM DIFFERENT THREAD PROTo illustrate how this works, we will review the code of the Image Pro application, a basic image processing application that allows the user to rotate, resize, and change the color depth of an image. Furthermore, since postEvent() is thread-safe, we can use it from any thread to post events to the GUI thread. Qt's event mechanism allows us to define custom event types in addition to the built-in types, and allows us to post events of these types using QApplication::postEvent(). The solution for communicating from a non-GUI thread to the GUI thread is to use custom events. But none of these techniques can be used to communicate with the GUI thread, since they would lock the event loop and freeze the user interface. If these new threads need to communicate among themselves, they can use shared variables together with mutexes, semaphores, or wait conditions. The GUI thread can start new threads by creating objects of a QThread subclass, as we did in the previous section. After the call to exec(), this thread is either waiting for an event or processing an event. For this reason, we normally refer to this thread as the GUI thread. This is the only thread that is allowed to create the QApplication object and call exec() on it. When a Qt application starts, only one thread is runningthe initial thread. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |