33

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?

NG_
  • 6,895
  • 7
  • 45
  • 67
Frank
  • 2,446
  • 7
  • 33
  • 67
  • 1
    Are you calling `app.exec()` (or whatever you've called the `QApplication`) from `main`? – tmpearce Jul 25 '12 at 14:20
  • 2
    You are also creating a memory leak, add `this` to the `QTimer` constructor. – cmannett85 Jul 25 '12 at 14:24
  • I added my main code. Adding this to the constructor hasn´t solved the problem. – Frank Jul 25 '12 at 14:28
  • 1
    @cmannett85 there is any memory leak, the MainWindow take the ownership of the timer, and when the MainWindow object be deleted, this will delete the timer on his destructor. – kato2 Jan 02 '18 at 21:32

3 Answers3

26

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...";
}
fpohtmeh
  • 506
  • 4
  • 15
17
  1. It's good practice to give a parent to your QTimer to use Qt's memory management system.

  2. update() is a QWidget function - is that what you are trying to call or not? http://qt-project.org/doc/qt-4.8/qwidget.html#update.

  3. If number 2 does not apply, make sure that the function you are trying to trigger is declared as a slot in the header.

  4. Finally if none of these are your issue, it would be helpful to know if you are getting any run-time connect errors.

fat
  • 6,435
  • 5
  • 44
  • 70
MrFox
  • 1,244
  • 1
  • 11
  • 24
  • 1
    Fixed it. The function apparently update() was not the best choise for the funtcion... When I changed it, it was fixed. – Frank Jul 25 '12 at 14:33
  • `update()` is overloaded several times, but the 'original' signature takes no arguments (scroll up slightly on the page you link to). Please amend your answer. – cmannett85 Jul 25 '12 at 18:03
  • `update` slot name works fine for me. Maybe the function has not been declared as a slot? – Pavel Strakhov Dec 21 '13 at 01:04
8

mytimer.h:

    #ifndef MYTIMER_H
    #define MYTIMER_H

    #include <QTimer>

    class MyTimer : public QObject
    {
        Q_OBJECT
    public:
        MyTimer();
        QTimer *timer;

    public slots:
        void MyTimerSlot();
    };

    #endif // MYTIME

mytimer.cpp:

#include "mytimer.h"
#include <QDebug>

MyTimer::MyTimer()
{
    // create a timer
    timer = new QTimer(this);

    // setup signal and slot
    connect(timer, SIGNAL(timeout()),
          this, SLOT(MyTimerSlot()));

    // msec
    timer->start(1000);
}

void MyTimer::MyTimerSlot()
{
    qDebug() << "Timer...";
}

main.cpp:

#include <QCoreApplication>
#include "mytimer.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Create MyTimer instance
    // QTimer object will be created in the MyTimer constructor
    MyTimer timer;

    return a.exec();
}

If we run the code:

Timer...
Timer...
Timer...
Timer...
Timer...
...

resources

Majid joghataey
  • 1,488
  • 2
  • 17
  • 28