Page 1 of 3 123 LastLast
Results 1 to 20 of 48

Thread: Multiple timers in single Application

  1. #1
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple timers in single Application

    Hello all,


    I m using Qt 4.4.3


    Can we add multiple timers in single Qt application.

    I done as following


    QTimer *timer;
    QTimer *timerEllipse;

    timer = new QTimer(this);
    timerEllipse = new QTimer(this);

    connect(timer, SIGNAL(timeout()), this, SLOT(RotateBG()));
    connect(timerEllipse, SIGNAL(timeout()), this, SLOT(ChangeEllipse()));

    RotateBG()
    {
    //Code to Rotate background
    }

    ChangeEllipse()
    {
    //Code to change color of ellipse
    }


    But only first timer function is executed every time ... why is it so??

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Hi,

    Where do you set timeout interval of timers and where do you start the timers?
    Is "ChangeEllipse" defined as slot? What return value are you getting on connection statment(use a boolean as return value) and the is there any error message on console output?
    Òscar Llarch i Galán

  3. #3
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Multiple timers in single Application




    The problem was that I forgot to add "ChangeEllipse" as slot and added as normal function in class.


    I changed it and now its working

    Thank you very much!!!!!!!

  4. #4
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Hi ^NyAw^,

    I am having the same problem. Can u help me plzz..
    As my application is having 6 timers with different intervals. The delay is occurring for updating the fields in the app.

    if i ran only 2 timers the updating speed is less than 80ms.

    if i run all 6 timers then updating speed of app is almost 4 secs.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Multiple timers in single Application

    The OP problem is sovled, if your problem is same then you already have a solution.

    Does all your timers complete in time ?

    t1+t2+t3+t4+t5+t6+system response time = response time (which is in your case is 4 seconds)
    tx = time taken by corresponding timer slot to execute

    t1+t2+t3+t4+t5+t6 should be less than the minimum timer value among all the 6 timer values
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    mahi6a1985 (20th September 2012)

  7. #6
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by Santosh Reddy View Post
    The OP problem is sovled, if your problem is same then you already have a solution.

    Does all your timers complete in time ?

    t1+t2+t3+t4+t5+t6+system response time = response time (which is in your case is 4 seconds)
    tx = time taken by corresponding timer slot to execute

    t1+t2+t3+t4+t5+t6 should be less than the minimum timer value among all the 6 timer values

    Hi,
    Here is what my problem indetail

    I am newbie to QT. As i am developing an application, got struck with the timers controls. I want to run 6 timers at time in some conditions. and in some conditions 3 timers.

    In this process 2 timers got delayed bcoz of the other as per my knowledge i think so.

    My timers n its intervals r as follows -

    HI all,

    I am newbie to QT. As i am developing an application, got struck with the timers controls. I want to run 6 timers at time in some conditions. and in some conditions 3 timers.

    In this process 2 timers got delayed bcoz of the other as per my knowledge i think so.

    My timers n its intervals r as follows -
    1st timer -
    timer_HMIScrLvl4 = new QTimer(this);
    connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
    timer_HMIScrLvl4->setInterval(100);
    2nd timer -
    Timer_failure_display = new QTimer(this);
    connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
    Timer_failure_display->setInterval(250);
    3rd timer -
    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);
    4th timer -
    Timer_diagonostic_refresh = new QTimer(this);
    connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
    Timer_diagonostic_refresh->setInterval(50);
    5th Timer -
    Timer_information_refresh = new QTimer(this);
    connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
    Timer_information_refresh->setInterval(25);
    6th timer -
    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);
    1st & 2nd timers are the main timers i should not wanted them to be delayed, they should run at a time.

    Thanks in Advance.

    Forgive me if any thing i posted is against to the rules if the forum.

    plzz help me if possible as early as possible.

  8. #7
    Join Date
    Jun 2010
    Location
    Pretoria, South Africa
    Posts
    22
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    How much processing is done in each of the slots you have. If all of these timer signals are being handled in the main thread then this might be your problem. Each event will only be executed once the previous one has completed.

    Try splitting the processing up into multiple threads.

  9. The following user says thank you to Robbie for this useful post:

    mahi6a1985 (20th September 2012)

  10. #8
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Hi Robbie,

    In each of the SLOT a lot of processing will be done.
    All the Timers will be started after a Pushbutton pressed.
    All these Timers are in my function

    ex:
    void MainWindow::def_Values(){
    timer_HMIScrLvl4 = new QTimer(this);
    connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
    timer_HMIScrLvl4->setInterval(100);

    Timer_failure_display = new QTimer(this);
    connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
    Timer_failure_display->setInterval(250);

    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);

    Timer_diagonostic_refresh = new QTimer(this);
    connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
    Timer_diagonostic_refresh->setInterval(50);

    Timer_information_refresh = new QTimer(this);
    connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
    Timer_information_refresh->setInterval(25);

    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);
    }

    All the above timers will started in pusbutton click event.

  11. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Multiple timers in single Application

    Then you have seriouly re-consider your structure / architectrue of using timers.

    1. Either simply the logic so that it can fit into the time
    or
    2. Try moving the heavy logic into a thread. (as other poster suggested)

    Some how I feel you can do this with using 6 timers or more, one important rule to follow is all the slots should complete with in time. Try re-structing your timers, and what they do.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. The following user says thank you to Santosh Reddy for this useful post:

    mahi6a1985 (22nd September 2012)

  13. #10
    Join Date
    Jun 2010
    Location
    Pretoria, South Africa
    Posts
    22
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    @mahi6a1985

    Do you really need 6 QTimers? I see from your code that there are two QTimers linked to the footer() slot.

    I would seriously consider moving your processing to different threads.

    When a QTimer emits the timeout signal, it gets put in the message queue and the slot is called by the event posting mechanism. Since all your QTimers are in the same thread, their timeout signals all get processed one after another.

    If, by chance, all the QTimers timeout at exactly the same time, then, the footer() slot will be called at least 925ms after update_HMIScrLvl4(). That's assuming that each of the slots instantaneously returned and called the subsequent timeout slot.

    I would suggest that you put each of your timer slots into its own thread, or group the processing by timeout interval. By this I mean that, because failure_display() and footer() are triggered every 250ms, their code could be executed in the same thread, if you like.

  14. The following user says thank you to Robbie for this useful post:

    mahi6a1985 (22nd September 2012)

  15. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    I have no idea what your application does but it looks to me you should seriously reconsider on how it should do it. For example you have this "information refresh" -- I don't know what it is supposed to do but assuming that at the end of its work someone is supposed to see some output from it, I strongly doubt he really needs to see it 40 times per second. From my own experience I can say that (unless you are doing something time critical, like steering a mars lander) it is enough if you refresh information on the user interface once per second or even less often. Same goes for "diagnostic refresh". Moreover you have three timers set at 250ms interval (two of which are calling the same method). Why not merge them into one timer?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    mahi6a1985 (22nd September 2012)

  17. #12
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    you can use Qt::concurrent run() functionality in your slots which will run in separate thread. and when your timers will stop ? I hope this should solve your problem ....

    FunctionThreads.cpp file

    void update_HMIScrLvl4(MainWindow *pWIndow)
    {
    /* you can access all public variables from Mainwindow class */
    while(!pWIndow->m_bStop)
    {
    do your work after finishing "pWIndow->m_bStop = true" then again start timers
    }
    }
    void failure_display(MainWindow *pWIndow)
    {

    }
    void diagonostic_refresh(MainWindow *pWIndow)
    {

    }

    MainWindow.cpp file

    extern void update_HMIScrLvl4(MainWindow *pWIndow);
    extern void failure_display(MainWindow *pWIndow);

    void MainWindow::def_Values(){
    timer_HMIScrLvl4 = new QTimer(this);
    connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
    timer_HMIScrLvl4->setInterval(100);

    Timer_failure_display = new QTimer(this);
    connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
    Timer_failure_display->setInterval(250);

    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);

    Timer_diagonostic_refresh = new QTimer(this);
    connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
    Timer_diagonostic_refresh->setInterval(50);

    Timer_information_refresh = new QTimer(this);
    connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
    Timer_information_refresh->setInterval(25);

    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);
    }


    void MainWindow::update_HMIScrLvl4()
    {
    QFuture<void>pWatcher;

    if(!pWatcher.isStarted())
    {
    pWatcher = QtConcurrent::run(update_HMIScrLvl4,this);
    }
    }
    void MainWindow::failure_display()
    {

    }
    Last edited by pradeepreddyg95; 22nd September 2012 at 05:48.

  18. The following user says thank you to pradeepreddyg95 for this useful post:

    mahi6a1985 (22nd September 2012)

  19. #13
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Thanks a lot guys.. Got some interesting points from you people.

    But i am sorry guys.. as i am new & also its my 1st QT application, i am unable to apply the points got from you as it is a huge application.

    Cant do anything with the programing...

    Quote Originally Posted by pradeepreddyg95 View Post
    you can use Qt::concurrent run() functionality in your slots which will run in separate thread. and when your timers will stop ? I hope this should solve your problem ....

    FunctionThreads.cpp file

    void update_HMIScrLvl4(MainWindow *pWIndow)
    {
    /* you can access all public variables from Mainwindow class */
    while(!pWIndow->m_bStop)
    {
    do your work after finishing "pWIndow->m_bStop = true" then again start timers
    }
    }
    void failure_display(MainWindow *pWIndow)
    {

    }
    void diagonostic_refresh(MainWindow *pWIndow)
    {

    }

    MainWindow.cpp file

    extern void update_HMIScrLvl4(MainWindow *pWIndow);
    extern void failure_display(MainWindow *pWIndow);

    void MainWindow::def_Values(){
    timer_HMIScrLvl4 = new QTimer(this);
    connect(timer_HMIScrLvl4, SIGNAL(timeout()), this, SLOT(update_HMIScrLvl4()));
    timer_HMIScrLvl4->setInterval(100);

    Timer_failure_display = new QTimer(this);
    connect(Timer_failure_display, SIGNAL(timeout()), this, SLOT(failure_display()));
    Timer_failure_display->setInterval(250);

    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);

    Timer_diagonostic_refresh = new QTimer(this);
    connect(Timer_diagonostic_refresh, SIGNAL(timeout()), this, SLOT(diagonostic_refresh()));
    Timer_diagonostic_refresh->setInterval(50);

    Timer_information_refresh = new QTimer(this);
    connect(Timer_information_refresh, SIGNAL(timeout()), this, SLOT(information_refresh()));
    Timer_information_refresh->setInterval(25);

    Timer_footer = new QTimer(this);
    connect(Timer_footer, SIGNAL(timeout()), this, SLOT(footer()));
    Timer_footer->setInterval(250);
    }


    void MainWindow::update_HMIScrLvl4()
    {
    QFuture<void>pWatcher;

    if(!pWatcher.isStarted())
    {
    pWatcher = QtConcurrent::run(update_HMIScrLvl4,this);
    }
    }
    void MainWindow::failure_display()
    {

    }

    i will try this & vil get back to you @pradeep.

  20. #14
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    If any compilation errors in this method plz inform us. ready to help ...

  21. #15
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Hi Pradeep,

    i got this compilation error

    xvd.jpg

  22. #16
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    give some different name to that function update_HMIScrLvl4
    eg: extern void func1();
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by pradeepreddyg95; 22nd September 2012 at 09:13. Reason: updated contents

  23. The following user says thank you to pradeepreddyg95 for this useful post:

    mahi6a1985 (22nd September 2012)

  24. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multiple timers in single Application

    Functions ran in threads cannot access the GUI (i.e. MainWindow). Furthermore this whole concept is simply illogical. If one has a function that takes 200ms to execute and wants to run it every 100ms then after 10 seconds one will have over 50 threads running doing the same thing, ruining each others work. Furthermore with QtConcurrent this will not work this way anyway, since QtConcurrent::run() uses the global thread pool which will very quickly dry up and those functions will not be executed every 100ms but only after a thread returns to the pool. To put it simple -- if you have a function that needs 200ms to complete, you cannot run it more often than once per 200ms, regardless if you use threads or not. You can even stand on your head and start juggling primed granades but this won't change the situation.
    Last edited by wysota; 22nd September 2012 at 09:08.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  25. #18
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    I have done what u said the compilation error was gone.
    but the while loop which u specified in the previous post, the i got the Compilation error now.

    xvd1.jpg


    Added after 10 minutes:


    Quote Originally Posted by wysota View Post
    Functions ran in threads cannot access the GUI (i.e. MainWindow). Furthermore this whole concept is simply illogical. If one has a function that takes 200ms to execute and wants to run it every 100ms then after 10 seconds one will have over 50 threads running doing the same thing, ruining each others work. Furthermore with QtConcurrent this will not work this way anyway, since QtConcurrent::run() uses the global thread pool which will very quickly dry up and those functions will not be executed every 100ms but only after a thread returns to the pool. To put it simple -- if you have a function that needs 200ms to complete, you cannot run it more often than once per 200ms, regardless if you use threads or not. You can even stand on your head and start juggling primed granades but this won't change the situation.
    Hi Wysota,

    Dear then suggest me one example with 6 timers with different slots with different timers and they should run at a time.
    -------------------------------------------------------------------------------------------------------------------------------
    The main concept of my app is through tcp/ip client - server App. in which my app is client & which always sends 1 telegram every 100ms using "update_HmiScrLvl4".

    and the telegram construction & values will produced by the other timers, so dat the constructed telegram can be sent to server using "update_HmiScrLvl4".

    and at the same time server responses to my clients 300ms telegram (i.e., 100ms 1st telegram no reply from server. 200ms telegram no reply from server. 300ms will get response from Server to client, 400ms no reply,500ms no reply, 600ms reply from server to client ------- 900ms -----1200ms-------- ) like this i vil get response from server.

    the response from server needs to evaluated using the "Timer_Information_Refresh".
    -------------------------------------------------------------------------------------------------------------------------------
    Last edited by mahi6a1985; 22nd September 2012 at 09:21.

  26. #19
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    read(), write() function should use with pMainwindow
    pMainWindow->read();
    pMainwindow->Write();
    decleare m_bStop in mainwindow.h public
    if we want we can access the GUI (mainwindow) in FunctionsThread.cpp also ... try with this you can get the result sure .....

  27. The following user says thank you to pradeepreddyg95 for this useful post:

    mahi6a1985 (22nd September 2012)

  28. #20
    Join Date
    Sep 2012
    Posts
    32
    Thanks
    18
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple timers in single Application

    Quote Originally Posted by pradeepreddyg95 View Post
    give some different name to that function update_HMIScrLvl4
    eg: extern void func1();
    ur testd.zip sample app was freezing bro when i run it.... y i cant able to know?

Similar Threads

  1. Single Application Instance
    By BadKnees in forum Qt Programming
    Replies: 8
    Last Post: 4th November 2014, 14:57
  2. Multiple project files in a single directory
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 17th July 2008, 07:03
  3. Single QGLContext across multiple QGLWidgets
    By pseudosig in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2008, 23:13
  4. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 06:53
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.