Qt5 Tutorial QTimer - 2020
In this tutorial, we will learn about QTimer.
The QTimer class provides repetitive and single-shot timers.
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.
In this example, we'll use Qt Console application.
We need to create a MyTimer class:
Here are the codes:
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 // MYTIMER_H
Note that MyTimer class is inherited from QObject. By inheriting from QObject, out class can use signal and slot mechanism Qt provides. Also, the macro Q_OBJECT set to make the MOC to function properly.
The MyTimerSlot() slot will be invoked when the timer signals timeout().
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..."; }
In the code, we creates QTimer object, and setup the signal and slot so that as soon as it the signal timeout() is emitted, our slot MyTimerSlot() prints out "Timer..." every seconds (1000ms).
void QTimer::start(int msec) starts or restarts the timer with a timeout interval of msec milliseconds. If the timer is already running, it will be stopped and restarted.
Finally, we need to make an instance of MyTimer class:
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... ...
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization