Results 1 to 6 of 6

Thread: Why isn't QTimer asynchronous?

  1. #1
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Why isn't QTimer asynchronous?

    I tried to use a simple `QTimer` object on my window widget so that I can calculate the elapsed time a method takes to complete.
    But to my astonishment! the timer was blocked until the method completes execution! i.e when the method in question ends, the timer starts ticking!!
    Here is a sample code to demonstrate what I wrote:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19. void on_btnTest_clicked();
    20. void OnTimerTick();
    21.  
    22. private:
    23. Ui::MainWindow *ui;
    24. ulong seconds;
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "opencv2/core/core.hpp"
    4. #include "opencv2/highgui/highgui.hpp"
    5. #include "opencv/cv.h"
    6. #include <QTimer>
    7. #include <QtCore>
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21.  
    22. void MainWindow::on_btnTest_clicked()
    23. {
    24. QTimer * timer = new QTimer(0);
    25. seconds =0;
    26. connect(timer,SIGNAL(timeout()),this,SLOT(OnTimerTick()));
    27.  
    28. timer->setInterval(100);
    29. timer->start();
    30.  
    31. QThread::sleep(5);//simulating a method which takes 5 seconds to complete
    32.  
    33. //timer->stop();
    34.  
    35. }
    36.  
    37. void MainWindow::OnTimerTick()
    38. {
    39. ui->lblElapsedTime->setText(QString::number(++seconds));
    40. }
    To copy to clipboard, switch view to plain text mode 
    How can I get the asynchronous behavior, something like what we have in C#? where the Timer runs its own thread of execution?

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why isn't QTimer asynchronous?

    For measuring the time a function call takes use QTime::elapsed() method instead of a QTimer object. Example:
    Qt Code:
    1. t.start();
    2. myFunction(...);
    3. std::cout << "myFunction(): " << t.elapsed() << "ms"
    To copy to clipboard, switch view to plain text mode 
    See also http://doc.qt.io/qt-5.4/qtime.html#start.
    Best regards
    ars

  3. The following user says thank you to ars for this useful post:

    Hossein (11th October 2015)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why isn't QTimer asynchronous?

    If you want the elapsed time display to update during the long running process then that process must relinquish control to the Qt event loop periodically (QApplication::processEvents()) or run in a separate thread, allowing the main thread to handle the GUI updates. How you put the long running process in a separate thread depends on the process a bit: see:
    http://doc.qt.io/qt-5/qtconcurrentrun.html
    http://doc.qt.io/qt-5/qthread.html
    And also the venerable but generally useful
    https://doc.qt.io/archives/qq/qq27-responsive-guis.html

  5. The following user says thank you to ChrisW67 for this useful post:

    Hossein (11th October 2015)

  6. #4
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why isn't QTimer asynchronous?

    thank you guys ,I appreciate it
    Quote Originally Posted by ChrisW67 View Post
    If you want the elapsed time display to update during the long running process then that process must relinquish control to the Qt event loop periodically (QApplication::processEvents()) or run in a separate thread, allowing the main thread to handle the GUI updates. How you put the long running process in a separate thread depends on the process a bit: see:
    http://doc.qt.io/qt-5/qtconcurrentrun.html
    http://doc.qt.io/qt-5/qthread.html
    And also the venerable but generally useful
    https://doc.qt.io/archives/qq/qq27-responsive-guis.html
    Yes thats exactly the case, Thanks I 'll have alook at it .
    By the way Is it not more logical to make QTimer run its own thread of execution in essence? since it will be literally emitting every x time, and most of the time this will be in conjunction with other stuff happening in parallel to it.
    In C# this concept is very well implemented and makes sense if you look at it.

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why isn't QTimer asynchronous?

    Quote Originally Posted by Hossein View Post
    By the way Is it not more logical to make QTimer run its own thread of execution in essence?
    That would just make things unnecessarily complex for the common use case where you just want to trigger a function every X milliseconds.
    Since it is easy enough to move a timer to a secondary thread, there is no need to make the common case more complex by forcing multithreading into the application.

    Your example demonstrates actually very well that multithreading is something one has to explicitly thing about.

    The primary objective should always be to keep the UI from becoming unresponsive. Blocking its thread with a long running operation is very bad for that goal.
    Instead of looking for a way to keep the timer responsive, you should be looking at keeping the UI responsive.

    The part that should run in the secondary thread is the part that blocks, the code that takes long to execute.


    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    Hossein (11th October 2015)

  9. #6
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why isn't QTimer asynchronous?

    Thanks, thats pretty reasonable,

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 20:47
  2. Replies: 15
    Last Post: 4th August 2012, 20:11
  3. QtSoap with asynchronous web service
    By TorAn in forum Qt Programming
    Replies: 0
    Last Post: 24th August 2010, 02:05
  4. asynchronous thread.
    By wookoon in forum Newbie
    Replies: 1
    Last Post: 7th July 2010, 08:16
  5. asynchronous vs. synchroneous
    By timmu in forum Qt Programming
    Replies: 4
    Last Post: 28th August 2009, 11:48

Tags for this Thread

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.