Qt learning path (20): Event receiving and ignoring

Source: Internet
Author: User

This chapter also describes Qt events. Perhaps this chapter cannot be a complete example, because events are always abstract, so it is better to understand them at the underlying layer!

The role of the event is described above. Let's take a look at how we can receive the event. Recall the previous code. We have rewritten the event function in the subclass so that these subclasses can complete some functions as needed, just like the following code:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> void MyLabel: mousePressEvent (QMouseEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> if (event-> button () = Qt :: leftButton ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> // do something
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>} else {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> QLabel: mousePressEvent (event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}


The code above is similar to the previous one. It is detected in the mouse-pressed event. If you press the left button to do our processing work, if it is not the left button, the function of the parent class is called. To some extent, the event is passed to the parent class for response. That is to say, the event is "ignored" in the subclass.

We can regard Qt event transmission as a chain: If the subclass does not process this event, it will continue to pass to other classes. In fact, Qt event objects all have an accept () function and ignore () function. Just like their names, the former is used to tell Qt that the event processing function "receives" the event and does not pass it any more. The latter tells Qt that the event processing function "ignores" the event, you need to continue the transfer and find another recipient. In the event processing function, you can use isAccepted () to check whether the event has been received.

In fact, we seldom use the accept () and ignore () functions. Instead, we just want to call the response function of the parent class if we want to ignore the event. As we once said, most events in Qt are protected. Therefore, the rewritten function must have a response function in its parent class. This method is feasible. Why? Because we cannot confirm that this handler function in the parent class has no operation, if we ignore the event directly in the subclass, Qt will not look for other recipients, then the operation of the parent class will not be performed, this may cause potential risks. In addition, let's take a look at the implementation of the QWidget's mousePressEvent () function:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> void QWidget: mousePressEvent (QMouseEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> event-> ignore ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> if (windowType () = Qt :: popup )){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> event-> accept ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> QWidget * w;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> while (w = qApp-> activePopupWidget ()) & w! = This ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> w-> close ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> if (qApp-> activePopupWidget () = w) // widget does not want to dissappear
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> w-> hide (); // hide at least
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> if (! Rect (). contains (event-> pos ())){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> close ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}


Note that the first statement will be ignored if all subclasses do not overwrite the mousePressEvent function. This implies that this component does not care about this event, this event may be passed to its parent component.

However, things are not absolute. In one case, we must use the accept () and ignore () functions, that is, when the window is closed. If you need a dialog box when the window is closed, you need to write it as follows:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> void MainWindow: closeEvent (QCloseEvent * event)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> if (continueToClose ()){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> event-> accept ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>} else {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> event-> ignore ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>
Bool MainWindow: continueToClose ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> if (QMessageBox: question (this,
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> tr (" Quit "),
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \ '# \' "/editer/InBlock.gif" alt = ""/> tr ("Are you sure to quit this application? "),
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/> QMessageBox: Yes | QMessageBox: No,
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> QMessageBox: No)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif" alt = ""/>== QMessageBox: Yes ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> return true;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>} else {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/> return false;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "alt =" "/>}


In this way, we can exit the program after inquiry.

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

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.