You can customize the event type. The simplest way is to specify the constant value of the Event Type through qevent: type, when creating the qcustomevent, create the structure data and send events through postevent () events. For example:
Const qevent: Type myevent = (qevent: type) 9393;
...
Qapplication: postevent (object, new qcustomevent (myevent ));
Self-built events must define Event Sequence (event number), and Custom Event Sequence sequence must be greater than the column Sequence Value of qevent: type, generally, less than 1024 of the values are reserved for the event type predefined by QT regression. The object is the receiver of the event. When the postevent () method is used, the event must be created in the new method. After the event is processed successfully, the event is automatically deleted, postevent () places the event to the end of the event detection column, and then returns immediately. You can use sendpostedevents () to immediately process events in the previous postevent () column ().
You can use the sendevent () method. The event is immediately sent to the receiver, and the event of the sendevent () method is not deleted. Therefore, the event is usually established in the stack region. For example:
Customevent event ("event message ");
Qapplication: sendevent (object, & event );
The self-generated event type must be a sub-class of the qevent. Generally, the qcustomevent type must be specified, if you create a self-built worm event, you can obtain more types of safety (type safety ).
To handle a user-defined event, you can redefine the customevent () method, for example:
Void customwidget: customevent (qcustomevent * event ){
Customevent * customevent = static_cast <customevent *> (event );
....
}
Or re-define the event () method to assign a self-generated event to another function or directly process it in event (). For example:
Bool customwidget: event (qevent * event ){
If (Event-> type () = mycustomeventtype ){
Customevent * myevent = static_cast <customevent *> (event );
// Handle self-initiated events or call other functions to handle events
Return true;
}
Return qwidget: event (event );
}
/*************************************** ***********************/
Well, I have reprinted so many other articles on event. Let me say two or three sentences.
This postevent has been plagued a few days ago, so I will mainly explain my personal understanding of postevent. I would like to mention other implementation mechanisms.
Messages are sent in QT through the following mechanisms: Signal and slot, postevent, and sentevent. Only synchronous sentevent is supported. Postevent can be implemented asynchronously. The Mechanism is to send messages to the message queue, and the message queue will throw these messages again (of course, we can also use the signal and slot mechanisms to implement this function, set the last parameter of the connect function to QT: queuedconnection ).
Let's get to the bottom. In the previous section, we talked about how to capture a message when the message queue throws the message? We only need to implement the event () or customevent () function in the parent class, and implement our own processing in it. We recommend using customevent () here ().
The following is a simple example:
# Include <qwidget>
# Include <qevent>
Const qevent: Type customevent_login = (qevent: type) 5001; // we recommend that you use a unique identifier above 5000.
Class postevent: Public qwidget
{
Q_object
Public:
Postevent (qwidget * parent = 0 );
~ Postevent ();
PRIVATE:
Void customevent (qevent * E); // This function is a virtual function of the parent class qwidget.
};
Postevent: postevent (qwidget * parent)
{
Qapplication: postevent (this, new qevent (customevent_login); // This function is used to send custom messages to the queue, and new qevent (customevent_login) can only be dynamically allocated, for the cause, see the postevent function description in the help document of QT.
}
Void postevent: customevent (qevent * E)
{
If (e-> type () = customevent_login) // Capture Messages
{
Qmessagebox msgbox;
Msgbox. settext ("the document has been modified .");
Msgbox.exe C ();
}
}
Well, I made a very idiotic mistake at the beginning, that is, using the customevent function as a user-defined function, but I do not know it is a virtual function in the parent class, therefore, no message is captured. Well, the above is just a simple application about postevent. If you want to learn more about message mechanisms, please read other articles about event.