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?