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 realize 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!
Aucun commentaire:
Enregistrer un commentaire