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...
...