QT Learning Pathway (19): Events (Event)

Source: Internet
Author: User

There are several standard dialogs, and there are no plans to continue to illustrate the use of some components, because they are difficult to finish and many things are related to the actual application. The complexity of the actual application determines that it is not possible to make all the usage of all components clear. This time, the nature of Qt's relatively advanced point: events.

An event is a system or QT itself that is emitted at different times. When the user presses the mouse, knocks down the keyboard, or the window needs to be redrawn, a corresponding event is emitted. Some events are emitted when responding to user actions, such as keyboard events, and other events are automatically emitted by the system, such as timer events.

In general, when using QT programming, we don't focus on events because in QT, events that require our attention always send a signal. For example, we are concerned about Qpushbutton mouse clicks, but we do not need to care about this mouse click event, but care about its clicked () signal. This is different from some of the other frameworks: in swing, all you have to care about is JButton's ActionListener this click event.

QT events are easily confused with signal slots. Here is a simple explanation, signal by the specific object, and then immediately to the Connect function connected to the slot processing; for events, QT uses an event queue to maintain all emitted events, and when new events are generated, they are appended to the end of the event queue. After the previous event completes, remove the following event for processing. However, when necessary, QT events can also be handled directly instead of entering the event queue. Also, events can be filtered using the event filter. In general, if we use components, we are concerned about the signal slot; If we customize the component, we are concerned about the event. Because we can change the default action of a component through events. For example, if we want to customize a Qpushbutton, then we need to rewrite its mouse click events and keyboard handling events, and send clicked () signals at the right time.

Remember that we created a Qapplication object in the main function and then called its exec () function? In fact, this function is the beginning of the Qt event loop. After the EXEC () function is executed, the program enters the event loop to listen for the application's events. When an event occurs, QT creates an event object. All the events of Qt are inherited from the Qevent class. After the event object is created, QT passes this event object to the Qobject event () function. The event () function does not directly handle events, but instead assigns to specific event-handling functions (the event handler) according to the type of the object. We will explain this in more detail in a later section.

In the parent class qwidget of all components, many event-handling functions are defined, such as keypressevent (), Keyreleaseevent (), Mousedoubleclickevent (), Mousemoveevent (), Mousepressevent (), mousereleaseevent () and so on. These functions are protected virtual, that is to say, we should redefine these functions in subclasses. Let's look at an example.

#include <qapplication>
#include <qwidget>
#include <qlabel>
#include < Qmouseevent>
Class Eventlabel:public Qlabel
{
protected:
void Mousemoveevent (Qmouseevent *event); void Mousepressevent (Qmouseevent *event);
void Mousereleaseevent (Qmouseevent *event);
};
void Eventlabel::mousemoveevent (qmouseevent *event)
{
This->settext (QString ("<center>

. Arg (Qstring::number (Event->x ()), Qstring::number (event-> Y ()));
}
void Eventlabel::mousepressevent (qmouseevent *event)
{
This->settext (QString ("<center> . Arg (Qstring::number (Event->x ()), Qstring::number ( Event->y ()));  
}
void Eventlabel::mousereleaseevent (qmouseevent *event)
{
QString msg;
msg.sprintf ("<center> This->settext (msg);  
}
int main (int argc, char *argv[])
{
Qapplication app (argc, argv);
Eventlabel *label = new Eventlabel;
Label->setwindowtitle ("MouseEvent Demo");
Label->resize (300, 200);
Label->show ();
return app.exec ();
}

Here we inherit the Qlabel class, rewriting the Mousepressevent, Mousemoveevent, and mousereleaseevent three functions. We did not add any functionality except to display the coordinates on the label when the mouse was pressed (press), mouse movement (move), and mouse release. Notice the construction of the qstring in the Mousereleaseevent function. Instead of using arg parameters, we construct qstring objects using the C-style sprintf, and if you are familiar with C syntax (it is estimated that many c+++ programmers will be familiar with it), then try the familiar C format in Qt.

Source: http://devbean.blog.51cto.com/448512/223974

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.