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

Design question for application using two QT timers



  • Dear Developers,

    In my QT application I have:

    1. Main application which monitor signal from QT Button widget.
    2. Timer 1 which I want to use to calculate delay of different time period.
      This delay I will use between execution of different instructions in program
    3. Timer 2 I need to observe status of hardware key after every
      one second and take some steps in case hardware key is pressed.

    So my question are:

    Q) What is correct way to design this application ?
    Whether I should use multiple thread in my application ?
    First thread to run main application and Timer1 (used to calculate delay).
    Second thread to run Timer 2, I am using to observe hardware key after every one second.

    Q) In case you recommend me that I can also achieve it using single thread application. Then what will happen if if

    1. I get signal from both QT Button widget and Timer 2 at same time? Whose slot will be executed first ?
    2. What will happen when I am in Button widget slot (approx have 10 second execution time) and I get signal from timer 2 ? Whether
      execution of Button slot will be interrupted and whether I will go in Timer 2 Slot?
    3. I want slot of timer 2 should have higher priority than Button Slot ? Can you please suggest me, how can I
      achieve it ?

    Thank you much. Kindly inform me if you need any information from me.



  • First - without threads is usually easier to make without problems. Go threading only if needed.

    Do you understand that a user interface is already kind of asynchronous, so that the user can do different things? Adding timers just adds more possible events. Timer events (signals) are handled just like UI signals. Whatever comes first is served by the system. Whatever you do in a slot takes as much time as it takes and without threads nothing else is done at the same time. The UI doesn't respond while your slot is executed. The same thing happens with timers. If the executing the slot takes much time you either have locked UI or you have to create a thread.

    So, it depends on how much time is spent executing the slot which receives the timer's signal. If it's almost instantaneous you probably don't have to care.



  • Hello Eeli K,

    Thank you very much for your quick reply. I think your answer has solved my query.

    thanks :)


Log in to reply