Analysis of QT Event Processing Mechanism

Source: Internet
Author: User

Http://blog.csdn.net/digu/archive/2007/09/26/1801074.aspx

 

If you have a little understanding of MFC, You can roughly map the QT signal (signal) and event (event) to the command and message of MFC ), events are the responses of the window system or QT to different situations. Most of the events generated are responses to user behaviors, but there are also some events, such as timer events, they are generated independently by the system. Qwidget: The event () virtual function is a manager of various events. It is responsible for passing most common types of events to a specific event processor (event processing functions are also virtual functions, to facilitate the inheritance of subclass programming for the event processing), you can analyze part of the source code of the event manager qwidget: event () virtual function (complete source code in C: /QT/4.2.2/src/GUI/kernel/qwidget. in CPP, qwidget: event () function definition ):
The qevent class encapsulates events. The qevent class is the base class of all event classes. Event objects contain event parameters.

Bool qwidget: event (qevent * event)
{
Q_d (qwidget );

// Ignore mouse events when disabled
If (! Isenabled ()){
Switch (Event-> type ()){
Case qevent: tabletpress:
Case qevent: tabletrelease:
Case qevent: tabletmove:
Case qevent: mousebuttonpress:
Case qevent: mousebuttonrelease:
Case qevent: mousebuttondblclick:
Case qevent: mousemove:
Case qevent: contextmenu:
# Ifndef qt_no_wheelevent
Case qevent: wheel:
# Endif
Return false;
Default:
Break;
}
}
Switch (Event-> type () {// judge the Event Type
Case qevent: mousemove:
Mousemoveevent (qmouseevent *) event); // assign a mouse-moving event to the mousemoveevent function for processing.
Break;

Case qevent: mousebuttonpress:
// Don't reset input context here. Whether reset or not is
// A responsibility of input method. Reset () will be
// Called by mousehandler () of input method if necessary
// Via mousepressevent () of text widgets.
# If 0
Resetinputcontext ();
# Endif
Mousepressevent (qmouseevent *) event );
Break;

Case qevent: mousebuttonrelease:
Mousereleaseevent (qmouseevent *) event );
Break;

Case qevent: mousebuttondblclick:
Mousedoubleclickevent (qmouseevent *) event );
Break;
# Ifndef qt_no_wheelevent
Case qevent: wheel:
Wheelevent (qwheelevent *) event );
Break;
# Endif
......
......
Then, rewrite the RESPONSE event processing function (overwrite the default virtual function, such as event and keypressevent) in the qwidget derived class that needs to process the event) to complete the response to the event in the derived class object.

The programming of event filters is generally divided into two steps:
1. register the Monitored object by calling the installeventfilter () function for the target object (Monitored object;
2. Process events of the target object in the eventfilter function of the Monitored object. (The Monitored object intercepts the event of the target object)

The following code describes event processing and Event Filtering:

// Newlineedit. h
# Ifndef newlineedit_h
# Define newlineedit_h

# Include <qlineedit>
# Include "ui_newlineedit.h"

Class newlineedit: Public qlineedit
{
Q_object

Public:
Newlineedit (qwidget * parent = 0 );
~ Newlineedit ();

Bool event (qevent * event); // rewrite the event virtual function to process specific events

PRIVATE:
Ui: newlineeditclass UI;
};

# Endif // newlineedit_h

// Www. h
# Ifndef www_h
# Define www_h

# Include <qtgui/qmainwindow>
# Include "ui_www.h"

Class www: Public qmainwindow
{
Q_object

Public:
WWW (qwidget * parent = 0, QT: wflags flags = 0 );
~ WWW ();

Bool eventfilter (qobject * target, qevent * event); // rewrite the eventfilter virtual function to intercept specific events.

PRIVATE:
Ui: wwwclass UI;
};

# Endif // www_h

// Newlineedit. cpp
# Include "newlineedit. H"
# Include <qkeyevent>

Newlineedit: newlineedit (qwidget * parent)
: Qlineedit (parent)
{
Ui. setupui (this );
}
Bool newlineedit: event (qevent * event)
{
If (Event-> type () = qevent: keypress)
{
Qkeyevent * keyevent = (qkeyevent *) event;
If (keyevent-> key () = QT: key_tab) // identifies a specific event and makes custom processing.
{
Insert ("/t ");
Return true;
}
}
Return qwidget: event; // send events that do not need to be processed to the default base class event () function for processing.
}
Newlineedit ::~ Newlineedit ()
{

}

// Www. cpp
# Include "www. H"
# Include <qkeyevent>

Www: WWW (qwidget * parent, QT: wflags flags)
: Qmainwindow (parent, flags)
{
Ui. setupui (this );
Ui. lineedit-> installeventfilter (this); // register this to monitor the UI. lineedit object. This intercepts some events of UI. lineedit.

Bool www: eventfilter (qobject * target, qevent * event)
{
If (target = UI. lineedit & event-> type () = qevent: keypress
& (Qkeyevent *) event)-> key () = QT: key_tab) // checks whether the monitoring interception conditions are met. If yes, the event is intercepted.
{
Ui. lineedit-> insert (TR ("filter completed! "));
Return true;
}
Return qmainwindow: eventfilter (target, event );
}

Www ::~ WWW ()
{

}

Next, let's analyze the source code of the qobject: installeventfilter function to see how it implements the filter function:
Void qobject: installeventfilter (qobject * OBJ)
{
Q_d (qobject );
If (! OBJ)
Return;

Qwritelocker locker (qobjectprivate: readwritelock ());

// Clean up unused items in the list
D-> eventfilters. removeall (qobject *) 0 );
D-> eventfilters. removeall (OBJ );
D-> eventfilters. prepend (OBJ );
}
Refer to the next article on qobject analysis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.