Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QTimer

Tags:

timer

qt

In Qt I'm trying to set a QTimer that calls a function called "update" every second. Here is my .cpp file:

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QTimer> #include "QDebug"  MainWindow::MainWindow(QWidget *parent) :     QMainWindow(parent),     ui(new Ui::MainWindow) {     ui->setupUi(this);      QTimer *timer = new QTimer(this);     connect(timer, SIGNAL(timeout()), this, SLOT(update()));     timer->start(1000); }  MainWindow::~MainWindow() {     delete ui; }  void MainWindow::update() {     qDebug() << "update"; } 

and the main:

#include <QtGui/QApplication> #include "mainwindow.h"  int main(int argc, char *argv[]) {     QApplication a(argc, argv);     MainWindow w;     w.show();      return a.exec(); } 

The project is being build, but it doesn't execute update, since the line "update" is not showing anywhere... Does anybody see what I´m doing wrong?

like image 756
Frank Avatar asked Jul 25 '12 14:07

Frank


People also ask

How does QTimer work?

The QTimer class provides timer signals and single-shot timers. It uses timer events internally to provide a more versatile timer. QTimer is very easy to use: create a QTimer, call start() to start it and connect its timeout() to the appropriate slots. When the time is up it will emit the timeout() signal.

How do you use QTimer in Python?

The QTimer class provides a high-level programming interface for timers. To use it, create a QTimer , connect its timeout() signal to the appropriate slots, and call start() . From then on, it will emit the timeout() signal at constant intervals.

How do I start a QT timer?

You can set a timer to time out only once by calling setSingleShot(true). You can also use the static QTimer::singleShot() function to call a slot after a specified interval: QTimer::singleShot(200, this, &Foo::updateCaption); In multithreaded applications, you can use QTimer in any thread that has an event loop.

How accurate is QTimer?

Most platforms support a resolution of 1 millisecond, though the accuracy of the timer will not equal this resolution in many real-world situations. The accuracy also depends on the timer type. For Qt::PreciseTimer, QTimer will try to keep the accurance at 1 millisecond.


1 Answers

Other way is using of built-in method start timer & event TimerEvent.

Header:

#ifndef MAINWINDOW_H #define MAINWINDOW_H  #include <QMainWindow>  namespace Ui { class MainWindow; }  class MainWindow : public QMainWindow {     Q_OBJECT  public:     explicit MainWindow(QWidget *parent = 0);     ~MainWindow();  private:     Ui::MainWindow *ui;     int timerId;  protected:     void timerEvent(QTimerEvent *event); };  #endif // MAINWINDOW_H 

Source:

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug>  MainWindow::MainWindow(QWidget *parent) :     QMainWindow(parent),     ui(new Ui::MainWindow) {     ui->setupUi(this);      timerId = startTimer(1000); }  MainWindow::~MainWindow() {     killTimer(timerId);     delete ui; }  void MainWindow::timerEvent(QTimerEvent *event) {     qDebug() << "Update..."; } 
like image 117
fpohtmeh Avatar answered Sep 21 '22 00:09

fpohtmeh