void ImageWindow::flipHorizontally()ĪddTransaction(new FlipTransaction(Qt::Horizontal)) Both of them involve signals emitted by the TransactionThread object, which we will cover in a moment. The interesting part of the ImageWindow constructor is the two signal–slot connections. StatusBar(), SLOT(showMessage(const QString &))) Ĭonnect(&thread, SIGNAL(allTransactionsDone()), StatusBar()->showMessage(tr("Ready"), 2000) Ĭonnect(&thread, SIGNAL(transactionStarted(const QString &)), ImageLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop) ImageLabel->setBackgroundRole(QPalette::Dark) The secondary 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 very large images. The application (shown in Figure 14.3), uses one secondary thread to perform operations on images without locking the event loop. #Qt update gui from different thread pro#To illustrate how signal–slot connections across threads work, 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. By default, a QObject exists in the thread in which it was created this can be changed at any time by calling QObject::moveToThread(). The slot is then called by the event loop of the thread in which the receiver object exists. (This behavior can be changed through an optional fifth parameter to QObject::connect().) Behind the scenes, these connections are implemented by posting an event. However, when we connect objects that "live" in different threads, the mechanism becomes asynchronous. Normally, the signals and slots mechanism operates synchronously, meaning that the slots connected to a signal are invoked immediately when the signal is emitted, using a direct function call. The solution for communicating from a secondary thread to the main thread is to use signal–slot connections across threads. But none of these techniques can be used to communicate with the main 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, read-write locks, semaphores, or wait conditions. The main 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. This is the only thread that is allowed to create the QApplication or QCoreApplication object and call exec() on it. When a Qt application starts, only one thread is running-the main thread. Learn More Buy Communicating with the Main Thread C++ GUI Programming with Qt4, 2nd Edition
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |