Important: Please read the Qt Code of Conduct -

QTimer in QThread. How?

  • Hello all!
    I've got stacked by simple issue. I need to implement QTimer in QThread and start and stop QTimer from another QThread. Officially in doc not possible to do it directly for QTimer from outside thread. I've been solving somehow it before, but I don't remember in precise how to do it. I remember only that was something about loop and making it through flag-system and onetime-timer inside of QThread. Could any one help me? Or correct me for simpler way of it.

  • Lifetime Qt Champion

    @bogong said in QTimer in QThread. How?:

    and stop QTimer from another QThread

    Use signals/slots between these two threads with queued connection type. In the thread where the timer is living you implement a slot and connect it to a signal from the second one.

  • @jsulm Am I right about understanding of it by this example?

  • Lifetime Qt Champion

    @bogong Yes, it does what I suggested. Doing it this way means that the timer is only accessed in the thread where it lives. Other threads only ask the timer thread to do something with the timer.

  • @jsulm Solution found. There was the error in example. I've been doing QThread inheritance but all of QTimer wasn't in run() function therefore it was running in main thread. Corrected version just published. Thx for assistance and refreshing memory.

    Issue closed.

Log in to reply