In the QT System, a GUI main event thread is running. The main thread obtains events from the window system and distributes them to various components for processing. In the qthread class, there is a method to submit an event to an object from a non-main event thread, that is, the qthread: postevent () method, this method provides a thread-safe event submission process in QT. The submitted event is put into a queue, and the GUI main event thread is awakened and sent to the corresponding object. This process is the same as the normal window system event processing process. When an event processing process is called, it is called in the main event thread, rather than in the thread that calls the qthread: postevent method.
1. Submission of system-defined events
In the QT System, many types of events are defined, such as timer events, mouse movement events, keyboard events, and window control events. Generally, events come from the underlying window system. Qt's main event loop function obtains these events from the system's event queue, converts them to qevents, and then passes them to the corresponding qobjects object.
As follows:
Qwidget * mywidget;
Void mythread: Run ()
{
Qthread: postevent (mywidget, new qpaintevent (qrect (0, 0, 100,100 )));
}
Send the re-painting event to the mywidget form class in the mythread thread.
The paintevent Event Response of mywidget is automatically called to respond to the re-painting event sent by mythread.
Void mywidget: paintevent (qpaintevent *)
{
}
2. Submit custom events
To meet users' needs, the QT System also provides a qcustomevent class for user-defined events. These custom events can use qthread: postevent () or qapplication: postevent () it is sent to various controls or other qobject instances, and the subclass of the qwidget class can easily receive these custom events through the qwidget: customevent () event handler function. Note that the qcustomevent object is created with a type ID to define the event type. To avoid conflict with the event type defined by the QT System, the ID value should be greater than the qevent of the enumeration type:: user value in type.
As follows:
The userevent class is a user-defined event class, and its event ID is 346798, which obviously does not conflict with the system-defined event type.
Class userevent: Public qcustomevent // user-defined event class
{
Public:
Userevent (qstring S): qcustomevent (346798), SZ (s ){;}
Qstring STR () const {return SZ ;}
PRIVATE:
Qstring SZ;
};
The userthread class is a subclass inherited from the qthread class. In addition to defining related variables and thread control functions, the main thing in this class is to define the thread startup function userthread: Run (), create a user-defined event userevent in this function, and use the postevent function of the qthread class to submit the event to the corresponding receiving object.
Class userthread: Public qthread // User-Defined Thread class
{
Public:
Userthread (qobject * r, qmutex * m, qwaitcondition * C );
Qobject * Cycler;
}
Void userthread: Run () // Thread class startup function, which creates a user-defined event
{Userevent * Re = new userevent (resultstring );
Qthread: postevent (REER, RE );
}
The userwidget class is a subclass of the User-Defined qwidget class used to receive custom events. This class uses the slotgo () function to create a new thread Recv (userthread class ), when a Custom Event (ID 346798) is received, the customevent function is used to process the event.
Void userwidget: slotgo () // member function of the User-Defined Control
{Mutex. Lock ();
If (! Recv)
Recv = new userthread (this, & mutex, & condition );
Recv-> Start ();
Mutex. Unlock ();
}
Void userwidget: customevent (qcustomevent * E) // user-defined event processing function
{If (e-> type () = 346798)
{
Userevent * Re = (userevent *) E;
Newstring = Re-> STR ();
}
}
In this example, a new thread userthread is created in the userwidget object. You can use this thread to implement some periodic processing (such as receiving messages sent from the underlying layer ), A user-defined event is submitted once a specific condition is met. When the userwidget object receives the event, it can handle the event as needed. Generally, the userwidget object can normally perform some routine processing without being affected by the underlying message.
Compilation of QT multi-thread Program
Prerequisites: a multi-threaded Library such as a libqt-mt.so (QT Library) or a libqte-mt.so (QTE Library) that has been compiled successfully)
In Windows: add an option to the qconfig. h file to define the macro qt_thread_support.
In Linux: add the multi-threaded library-lqt-Mt or-lqte-MT to the link options in makefile, and add-dqt_thread_support to the compilation options to add thread support for macro qt_thread_support.
See http://blog.pconline.com.cn/article/393450.html for details