Qt learning path (19): event)

Source: Internet
Author: User

I have mentioned several standard dialogs. I do not intend to continue to describe the use of some components, because these are difficult to complete and many things are related to the actual application. The complexity of the actual application makes it impossible for us to describe all the usage methods of all components. This time we will talk about the relatively advanced features of Qt: events. Events are issued at different times by the system or Qt itself. When you press the mouse, press the keyboard, or re-draw the window, a corresponding event is triggered. Some events are triggered in response to user operations, such as Keyboard Events. Others are automatically triggered by the system, such as timer events. In general, we do not focus on events when using Qt programming, because in Qt, events that require us to be concerned always send a signal. For example, we care about QPushButton's mouse clicking, but we don't need to care about this mouse clicking event, but about its clicked () signal. This is different from other frameworks: In Swing, you need to care about the Click Event ActionListener of the JButton. Qt events are easily confused with the signal slot. Here, we will briefly describe that signal is issued by a specific object and will be immediately handed over to the slot connected by the connect function. For events, qt uses an event queue to maintain all issued events. When a new event is generated, it is appended to the end of the event queue. After the previous event is completed, extract the following events for processing. However, if necessary, Qt events can be processed directly without going into the event queue. In addition, you can use the "Event Filter" to filter events. In general, if weUseComponent, we care about the signal slot; if weCustomComponents, we are concerned about events. Because we can use events to change the default operations of components. For example, if we want to customize a QPushButton, We need to rewrite its mouse click event and keyboard processing event, and send a clicked () signal 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 starts the Qt event loop. After the exec () function is executed, the program enters the event loop to listen to the events of the application. When an event occurs, Qt creates an event object. All events of Qt are inherited from the QEvent class. After the event object is created, Qt passes the event object to the event () function of QObject. The event () function does not directly process events, but assigns the event handler to a specific event handler according to the event object type ). We will detail this in a later chapter. In the parent class QWidget of all components, many event processing functions are defined, such as keyPressEvent (), keyReleaseEvent (), mouseDoubleClickEvent (), mouseMoveEvent (),MousePressEvent () and mouseReleaseEvent. These functions are all protected virtual, that is, we should redefine these functions in the subclass. The following is an example. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> # include <QApplication>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> # include <QWidget>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> # include <QLabel>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> # include <QMouseEvent>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> class EventLabel: public QLabel
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> protected:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> void mouseMoveEvent (QMouseEvent * event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> void mousePressEvent (QMouseEvent * event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> void mouseReleaseEvent (QMouseEvent * event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> void EventLabel: mouseMoveEvent (QMouseEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif"/> this-> setText (QString ("<center> 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif"/>. arg (QString: number (event-> x (), QString: number (event-> y ())));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> void EventLabel: mousePressEvent (QMouseEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif"/> this-> setText (QString ("<center> 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif"/>. arg (QString: number (event-> x (), QString: number (event-> y ())));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> void EventLabel: mouseReleaseEvent (QMouseEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> QString msg;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif"/> msg. sprintf ("<center> 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> event-> x (), event-> y ());
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> this-> setText (msg );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> int main (int argc, char * argv [])
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> QApplication app (argc, argv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> EventLabel * label = new EventLabel;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> label-> setWindowTitle (" MouseEvent Demo ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> label-> resize (300,200 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> label-> show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/> return app.exe c ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1S3504153-0.gif "/>}
Here we inherit the QLabel class and override the three functions: mousePressEvent, mouseMoveEvent, and MouseReleaseEvent. We didn't add any functions. We only showed the coordinates on the Label when pressing, moving, and release. Note that we construct QString in the mouseReleaseEvent function. Instead of using the arg parameter, we use C-language sprintf to construct a QString object, if you are familiar with C syntax (many C ++ programmers are familiar with it), you can try the familiar C format Writing Method in Qt!

This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/223974

Related Article

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.