Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QEventLoop Confusion



  • Howdy,

    I'm very confused. My app has a separate comms thread, and I'm trying to make sure that a particular event source is being put (via Objective C) onto that thread's event loop. Is there a way to get a pointer to a QThread's event loop?

    Assumptions: Every QThread has its own QEventLoop. (Or do you have to create one? I've seen both asserted.) A thread's event loop is kicked off by calling exec() in the thread, which is done automatically when the thread's run() method is called, which happens automatically when start() is called on the thread. (Or is this not true? I've found documentation saying both that you must call run() and exec() yourself, and also that they are nested as I described.)

    I've seen the functions to check if an event loop isRunning(), but I don't see any way to call this function without a pointer to the event loop in question, and QThread seems to have no accessor function for its default event loop. Trying to compare the comms thread's event loop to the Objective C run loop I'm trying to hook seems like it should be a thing I can do, but I'm completely defeated at this point.

    Thanks,
    Alex


  • Moderators

    @wumpus7 said in QEventLoop Confusion:

    Is there a way to get a pointer to a QThread's event loop?

    Not in the Qt API.

    Assumptions: Every QThread has its own QEventLoop.... A thread's event loop is kicked off by calling exec() in the thread

    Correct.

    To be more precise, the QEventLoop is created inside QThread::exec() and it is destroyed when QThread::exec() returns: https://code.woboq.org/qt5/qtbase/src/corelib/thread/qthread.cpp.html#_ZN7QThread4execEv

    which is done automatically when the thread's run() method is called, which happens automatically when start() is called on the thread. (Or is this not true? I've found documentation saying both that you must call run() and exec() yourself, and also that they are nested as I described.)

    It depends.

    If you use a QThread without subclassing it, then QThread::start() calls QThread::run() which calls QThread::exec() as you described.

    However, if you subclass QThread and reimplement QThread::run(), then you must call exec() in your implementation of run().

    I've seen the functions to check if an event loop isRunning(), but I don't see any way to call this function without a pointer to the event loop in question

    See QThread::isRunning() and QThread::isFinished()

    QThread seems to have no accessor function for its default event loop.

    Correct. The QEventLoop only exists inside QThread::exec() and it is not accessible from outside.

    Trying to compare the comms thread's event loop to the Objective C run loop I'm trying to hook seems like it should be a thing I can do, but I'm completely defeated at this point.

    I have no experience with accessing event loops from Objective-C, but it is possible to subclass QThread and create your own custom QEventLoop instead of calling QThread::exec(). This is very hacky though and not something I'd normally recommend -- it breaks the QThread API like QThread::isRunning().


Log in to reply