How can I set timer in order to change the picture in the label in Qt
-
I'm trying to change the picture in the label after the time interval as 12s. But it won't work, can anyone please help! The code is attached. Besides that, due to the picture size is large, thus it cannot use QVector< QPixmap > picArray to store the picture. Is there any way to realise what I wish ? Appreciate for the help!
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QWidget> #include <QMouseEvent> #include <QVector> #include <QPixmap> #include <QTimer> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); void mouseMoveEvent(QMouseEvent *event); void update(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; QVector<QPixmap> picArray; QTimer *timer; int picCounter; int timerInterval; }; #endif // MAINWINDOW_H
This is the code for mainwindow.h
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->lineEdit->setStyleSheet("QLineEdit {color: white;}"); setMouseTracking(true); ui->centralWidget->setMouseTracking(true); timer = new QTimer; picCounter = 0; timerInterval = 12000; connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(timerInterval); } void MainWindow::mouseMoveEvent(QMouseEvent *event) { setMouseTracking(true); ui->lineEdit->setText(QString(tr("move to:(%1, %2)")).arg(QString::number(event->x()), QString::number(event->y()))); } void MainWindow::update() { timer->setInterval(timerInterval); QPixmap p0(":/movies/ralph.png"); QPixmap p1(":/movies/polis.png"); QPixmap p2(":/movies/robin.png"); if (picCounter == 0) { ui->label_2->setPixmap(p0); ui->label_2->setScaledContents(true); ui->label_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); } else if (picCounter == 1) { ui->label_2->setPixmap(p1); ui->label_2->setScaledContents(true); ui->label_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); } else if (picCounter == 2) { ui->label_2->setPixmap(p2); ui->label_2->setScaledContents(true); ui->label_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); } // update picture picCounter++; if (picCounter == 3) picCounter = 0; } MainWindow::~MainWindow() { delete ui; }
This is the code for mainwindow.cpp and the picture is stored under the resource file (qrc)
Hope to get some help! Thanks a lot!
-
@greencow said in How can I set timer in order to change the picture in the label in Qt:
void update();
your slot is not declared as slot therefore the connect can't work. You will also get a warning during the runtime of your program about this. Also I won't call the function update() since this is also a function of QWidget iirc.
You should use the new signal/slot syntax nowadays to avoid such errors like above:
connect(timer, &QTimer::timeout, this, &MainWindow::update);
thus it cannot use QVector< QPixmap > picArray
Don't understand this... esp. since you're loading all pictures in your update function every time no matter what picture is really used later on which is for sure more expensive then string them all in a vector.
-
@Christian-Ehrlicher Thank you for replying ! I'm new to this ide, a lot of things unfamiliar. Thanks for pointing out the mistake! I really appreciate it! The program can now work as what I wish to! Thank you! One more thing I would like to ask how I can make the size of the push button increase when the mouse/cursor on it ? What I did is using the coordinate/position such that if the mouse/cursor in between the region of the push button then the size of push button change but it won't work.
-
Please mark this issue to solved. Also for your other question please raise one more topic.
-
@dheerendra Okay, done, thank you for reminding!