Timer
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTimer>
#include <QTimerEvent>
#include <QWidget>
class MyUpdateTimer : public QObject
{
Q_OBJECT
public:
explicit MyUpdateTimer(QWidget *widget, QObject *parent = nullptr)
: QObject(parent)
, m_widget(widget)
{
}
void start()
{
startTimer(0);
}
protected:
void timerEvent(QTimerEvent *event) override
{
Q_UNUSED(event);
m_widget->repaint();
}
private:
QWidget *m_widget;
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget w;
auto layout = new QVBoxLayout(&w);
auto btn = new QPushButton("Create QTimer", &w);
QObject::connect(btn, &QPushButton::clicked, &w, [&w]() {
auto timer = new QTimer(&w);
QObject::connect(timer, &QTimer::timeout, &w, [&w] { w.repaint(); });
timer->setInterval(0);
timer->start();
});
layout->addWidget(btn);
btn = new QPushButton("Start Timer", &w);
QObject::connect(btn, &QPushButton::clicked, &w, [&w]() {
auto timer = new MyUpdateTimer(&w, &w);
timer->start();
});
layout->addWidget(btn);
w.show();
return app.exec();
}
#include "timer.moc"