Java Event Processing Mechanism
1. Develop Custom Event classes.
Public class propertyevent extends eventobject {
Public propertyevent (){}
}
2. Define the listener interface.
Public interface propertylistener extends eventlistener {
Public void propertychanged (propertyevent );
}
3. Define the interface of the event initiator.
Public void add listener (listener );
Public void remove listener (listener );
Protected void process event (event );
4. Events are triggered.
5. Implement the listener interface.
MFC practice
Afx_msg void onappabout ();
Declare_message_map ()
Begin_message_map (ctapp, cwinapp)
On_command (id_app_about, onappabout)
End_message_map ()
Use Events in c #
1. Create a delegate Public Delegate void delegateme (Object PARAM );
2. Associate the created delegate with a specific event public event delegateme eventme;
3. Write the event handler public void Policy (); policyeveryone ();
4. Use the compiled event handler to generate a delegate instance DelegateEvent eventSource = new DelegateEvent ();
5. Add the delegated instance to the event list that generates the event object. This process is also called subscription event eventSource. ~yeveryone + = new event_test.DelegateEvent.MyDelegate (objA. DispMethodA );
6. Use this event eventSource. Y ();
QT event processing
When the internal state of an object changes, the signal is sent. In some ways, it may be interesting for the object proxy or owner. Only the class that defines a signal and its subclass can transmit this signal.
Class Foo: public
QObject
{
Q_OBJECT
Public:
Foo ();
Int value () const {return val ;}
Public slots:
Void setValue (int); // slot
Signals:
Void valueChanged (int); // Signal
Private:
Int val;
};
When a signal connected to the slot is sent, the slot is called. Slots are common C ++ functions and can be called like them. Their only feature is that they can be connected by signals. The Slot Parameter cannot contain the default value, and is the same as the signal. It is unwise to use a specific type for slot parameters.
Foo a, B;
Connect (& a, SIGNAL (valueChanged (int), & B, SLOT (setValue (int); // connect
B. setValue (11 );
A. setValue (79 );
B. value ();
Message Processing Process of QT
Bool MyClass: event (
QEvent * E ){
If (e-> type () = QEvent: KeyPress ){
QKeyEvent * Ke = (QKeyEvent *) e;
If (ke-> key () = Key_Tab ){
// Specific tab Processing
K-> accept ();
Return TRUE;
}
} Else if (e-> type ()> = QEvent: User ){
QCustomEvent * C = (QCustomEvent *) e;
// Here is the Custom Event Processing
Return TRUE;
}
QWidget: event (E );
}