Qt learning path (23): Custom events

Source: Internet
Author: User

This part ends with the Qt event. We have previously learned about the event mechanism of Qt. The following describes how to customize events.

Qt allows you to create your own event types, which is particularly useful in multi-threaded programs. Of course, it can also be used in a single-threaded program as a mechanism for inter-object communication. So why do I need to use events instead of signal slots? The main reason is that event distribution can be both synchronous and asynchronous, while function calls or slot Callbacks are always synchronous. Another benefit of an event is that it can use a filter.

The custom events in Qt are very simple. Similar to other similar libraries, they all inherit a class for extension. In Qt, the class you need to inherit is QEvent. Note: In Qt3, the class you want to inherit is QCustomEvent, but this class has been abolished in Qt4 (here, it is not recommended to be abolished, rather than deleted from the class library ).

To inherit from the QEvent class, you must provide a QEvent: Type parameter as the Type value of the Custom Event. QEvent: The Type is an enum defined in QEvent. Therefore, you can pass an int. It is important that your event type cannot be the same as an existing type value; otherwise, an unexpected error may occur! Because the system will dispatch and call your events as system events. In Qt, the system retains the value 0-999, that is, your event type must be greater than 999. specifically, the type of your custom event must be in the range of QEvent: User and QEvent: MaxUser. The QEvent: User value is 1000, and the QEvent: MaxUser value is 65535. You can define a maximum of 64536 events. I believe this number is large enough! However, even so, you can only ensure that user-defined events cannot overwrite system events, and that custom events cannot be overwritten. To solve this problem, Qt provides a function: registerEventType () for registering custom events. The function signature is as follows:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif"/> static int QEvent: registerEventType (int hint =-1 );
The function is static, so you can use the QEvent class to call it directly. The function accepts an int value. The default value is-1. The return value is the value of the Type created. If the hint is valid and does not overwrite, the value is returned. If the hint is invalid, the system automatically allocates a valid value and returns it. Therefore, you can use this function to specify the type value. This function is thread-safe, so there is no need to add synchronization.

You can add the data required for your event in the QEvent subclass and send the event. Qt provides two sending methods:

  • Static bool QCoreApplication: sendEvent (QObjecy * receiver, QEvent * event): the event is directly sent to the worker er object by the notify () function of QCoreApplication. The returned value is the return value of the event processing function. To use this function, you must create an object on the stack, for example: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif"/> QMouseEvent event (QEvent: MouseButtonPress, pos, 0, 0, 0 );
    650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/> QApplication: sendEvent (mainWindow, & event );
  • Static bool QCoreApplication: postEvent (QObject * receiver, QEvent * event): the event is appended to the end of the event list by QCoreApplication and is waiting for processing. This function will return immediately after appending the event, note that this function is thread-safe. Another point is that using this function must create an object on the stack, for example: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif"/> QApplication: postEvent (object, new MyEvent (QEvent :: registerEventType (2048); this object does not need to be manually deleted, and Qt will automatically delete it! Therefore, if you call delete after the post event, the program may crash. In addition, the postEvent () function also has an overloaded version, adding a priority parameter. For details, see API. You can call the sendPostedEvent () function to process submitted events immediately.
To process custom events, you can override the customEvent () function of QObject, which receives a QEvent object as a parameter. Note that this parameter is of the QCustomEvent type in Qt3. You can rewrite the event () function as described earlier:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/> void CustomWidget: customEvent (QEvent * event ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif"/> CustomEvent * customEvent = static_cast <CustomEvent *> (event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/> //....
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/>}
In addition, you can rewrite the event () function to process custom events:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/> bool CustomWidget: event (QEvent * event ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif"/> if (event-> type () = MyCustomEventType ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = \' # \ '"/editer/InBlock.gif"/> CustomEvent * myEvent = static_cast <CustomEvent *> (event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/> // processing...
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/> return true;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/>
Return QWidget: event (event );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src = \ '# \'"/editer/InBlock.gif "/>}
Both methods are feasible.

Now, we have briefly introduced the Qt event mechanism, including a series of issues such as event distribution and customization. The following sections will continue our learning journey!

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

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.