简体   繁体   中英

How do events work in Qt?

I have some questions about the general usage of Qt events. I'm new to Qt, and I am going to try out making a custom event. The questions I ask are related to this article: Qt 4.8: The Event System

  • When does an event “occur”?
    In Qt's built in events, mouse clicks and key presses are put into a queue and then the functions are executed at the next frame. I'm especially wondering about this for custom events, because I'm not sure if it always occurs when a mouse is clicked. For custom events, is it when you send the event into the queue and then waits to be processed by the event() function at the next frame?

  • To create a custom event, must you make a class that inherits from QEvent and register an event with registerEventType() function?
    Is this the standard process, or can an event simply be a class? How does this function, registerEventType(), work?

  • Do custom events have a QEvent::Type?
    Is this the number between 1000 and 6563 that is given when using the registerEventType() function?

  • What is the recipient of an event?
    It seems to be in functions' parameters like postEvent(), sendEvent(), etc. However, I am not sure what this object is for.

  • Where is the actual code that is executed when an event is fired?
    Is it a function, or is it in the event() function of QObjects.


Also any working examples of Qt events (both built-in or custom) in action would be helpful.

You should look at Qt Examples online or in QtSDK, there are tons of them. IMO you didn't search hard.

Ad.1. It occurs after you fire it with QApplication::postEvent() or QApplication::sendEvent(). Not immediately of course, because it has to go through the main loop, etc. The order of events should be preserved, though.

Ad.2. Look at this , second anwser.

Ad.3. Go to Ad.2.

Ad.4. This object will receive this custom event in QObject::customEvent() handler.

Ad.5. Go to Ad.4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM