#include#include class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateUI())); timer->start(1000); // Set the timer interval to 1000ms or 1s } public slots: void updateUI() { qDebug() << "Updating user interface..."; } };
#includeIn this example, we create a QLabel object and set its pixmap to an image file. We then create a QTimer object and connect its timeout signal to a user-defined slot function called animateLabel(). We start the timer with an interval of 1000ms or 1s. When the timer times out, the animateLabel() function will be called and the QLabel object will be animated by moving it around the parent widget using the move() function. The package library for QTimer is the Qt library, specifically the QtCore module. To use QTimer, you will need to include the necessary header files and link against the Qt library.#include #include class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { QLabel *label = new QLabel(this); QPixmap pixmap("image.png"); label->setPixmap(pixmap); label->setFixedSize(pixmap.size()); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(animateLabel())); timer->start(1000); // Set the timer interval to 1000ms or 1s } public slots: void animateLabel() { QLabel *label = qobject_cast (sender()->parent()); if (label) { static int x = 0; static int y = 0; int dx = 5; int dy = 5; if (x + dx > width() - label->width() || x + dx < 0) dx = -dx; if (y + dy > height() - label->height() || y + dy < 0) dy = -dy; x += dx; y += dy; label->move(x, y); } } };