Important: Please read the Qt Code of Conduct -

Event loop

  • Hello all
    I am a new user to qt.
    I wanted to know about qt event loop.
    When is a new event loop started?
    Which event queue does this use . I mean does each event loop has its own event queue or all share one?

    How to check which event loop executed an event . Like is there some mechanism which can be used to assign an id to event loop and hence print it in the event loop to check which event loop is executing this event?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The application event loop is started when you call:

    QApplication app(argc, argv);
    QWidget w;;
    return app.exec(); << here it starts

    You can have secondary event loops tarted when e.g. you call a QDialog's exec function. The events are processed by the current event loop.

    If you want more details about the internals, you should directly read Qt's sources.

    Hope it helps

  • Moderators

    QThread's also have their own event loop. And you can create your own event loops with QEventLoop at any time and handle all events with a call to QEventLoop::exec(). Not a whole lot of reasons to do this but the functionality is there if you need it.

    You wouldn't have anything to gain from assigning an ID to an eventloop to see which one was processing the event since you already know based on what loop is running (exception being a QThread loop but you should know which ones it is processing as well based on what signals/slots you connected to the thread).

    Event loops are blocking calls (usually on exec()) so any event that happens will be processed by the currently "blocking" event loop. So if you are running an application event loop app.exec() then it will get all the events. If you start up a modal dialog at some point QDialog::exec() then it will now be getting all the events until it exits.

    The only time this gets a bit confusing is when you are dealing with threads. That is a whole different topic though. I won't go into it here unless you really need me to.

  • This is a good information.
    Can you share a code snippet of how could qeventloop can be overwritten.

    Also i understood that there is only 1 event queue which has events posted on it , the event loop which will take up the event will be the one which is currently in picture. I this right statement ? Like if i have an event posted at time X then exec a dialog box ,
    So will the event loop exexed by dialog will process the paintevent posted at time X OR it will only process events coming after it is execed?

  • Moderators

    You can get more information on overriding event loops from the Qt docs on QEventLoop. I've never needed to do that so I don't have a snippet for you. Not really sure why you would want to have a custom event loop, but it is possible using QEventLoop.

    Typically events posted before an event loop is started are not handled by the new event loop. Although I'm guessing there. I don't know that for sure. If you really want to dig into event loops, you should check out Qt's code itself. In my 15 years of Qt I've never needed to make a custom event loop. It's all handled by the modal object, the application, or the thread.

  • if you want to know what object called the event, who emitted the signal, you might want to use sender(), but please tell give more info about you issue.

  • Lifetime Qt Champion

    @yoavmil beware, signals/slots and events are not the same beast. Emitting a signal doesn't mean there will be an event. i.e. if both objects leaves in the same thread it won't or if the connection type is forced to direct connection.

Log in to reply