QT Learning Pathway (23): Customizing events

Source: Internet
Author: User

QT allows you to create your own event types, which are especially useful in multithreaded programs, and, of course, can be used in single-threaded programs as a mechanism for communicating between objects. So why do I need to use the event instead of using a signal slot? The main reason is that the distribution of events can be synchronous and asynchronous, and the call to the function or the callback of the slot is always synchronized. Another benefit of the event is that it can use a filter.

The custom events in QT are very simple, similar to the use of other similar libraries, to inherit a class to extend. In Qt, the class you need to inherit is qevent. Note that in Qt3, the class you need to inherit is qcustomevent, but this class has been abolished in QT4 (the abolition here is not recommended, not removed from the class library).

To inherit the Qevent class, you need to provide a qevent::type type of parameter as the type value of the custom event. The Qevent::type type here is an enum defined within the qevent, so you can pass an int. The important thing is that your event type cannot be repeated with the existing type value, otherwise unpredictable errors will occur! Because your event will be distributed and invoked as a system event. In Qt, the system retains a value of 0-999, which means that your event type is greater than 999. Specifically, the type of your custom event should be between the Qevent::user and the Qevent::maxuser range. Where the value of the Qevent::user value is 1000,qevent::maxuser is 65535. Know from here, you can define up to 64,536 events, believe that this number is big enough! However, even so, it is only guaranteed that user-defined events cannot overwrite system events and do not guarantee that custom events will not be overwritten. To solve this problem, QT provides a function: Registereventtype (), which is used to customize the registration of events. The function is signed as follows:

static int QEvent::registerEventType ( int hint = -1 );

Functions are static, so you can call them directly using the Qevent class. The function accepts an int value with a default value of-1, and the return value is the value of the type that was created. This value is returned if the hint is legal and no overwrite occurs, and if hint is not valid, the system automatically assigns a legal value and returns it. Therefore, you can use this function to specify the type value. This function is thread-safe, so you do not have to add additional synchronizations.

You can add the data you need for your event in the Qevent subclass, and then send the event. There are two modes of delivery in QT:

* static bool Qcoreapplication::sendevent (Qobjecy * receiver, Qevent * Event): Qcoreapplication of events Notify () function is sent directly to the receiver object, and the return value is the return value of the event handler function. To use this function, you must create an object on the stack, for example:

Qmouseevent Event (qevent::mousebuttonpress, POS, 0, 0, 0);

Qapplication::sendevent (MainWindow, &event);

* static bool Qcoreapplication::p ostevent (QObject * receiver, Qevent * Event): event is appended to the end of the list of events by Qcoreapplication, and waits for processing, This function appends the event and returns immediately, and note that the function is thread safe. Another point is that using this function you must create an object on the heap, for example:

Qapplication::p ostevent (object, New MyEvent (Qevent::registereventtype (2048));

This object does not need manual DELETE,QT will automatically delete! 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, see API. By calling the Sendpostedevent () function, you can let the committed event be processed immediately.

If you are working with custom events, you can override the Qobject customevent () function, which receives a Qevent object as an argument. Note that this parameter is qcustomevent type in QT3. You can override this function by rewriting the event () function as described earlier:

void CustomWidget::customEvent(QEvent *event) {
 CustomEvent *customEvent = static_cast<CustomEvent *>(event);
 // ....
}

Alternatively, you can work with custom events by overriding the event () function:

bool CustomWidget::event(QEvent *event) {
 if (event->type() == MyCustomEventType) {
  CustomEvent *myEvent = static_cast<CustomEvent *>(event);
   // processing...
  return true;
 }
  return QWidget::event(event);
}

Both of these approaches are feasible.

Well, so far, we have outlined the QT event mechanism, including the distribution of events, customization, and a series of issues. The following chapters will continue our learning path!

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

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.