【问题标题】:How can I set timer in order to change the picture in the label in Qt如何设置计时器以更改 Qt 中标签中的图片
【发布时间】:2018-11-24 17:01:10
【问题描述】:

我正在尝试在时间间隔为 12 秒后更改标签中的图片。但它不会工作,任何人都可以请帮助!附上代码。另外,由于图片很大,所以不能使用QVector< QPixmap > picArray来存储图片。

有什么方法可以实现我的愿望吗?

感谢您的帮助!

#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

这是 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;
}

这是mainwindow.cpp的代码,图片存放在资源文件(qrc)下

希望能得到一些帮助!非常感谢!

【问题讨论】:

    标签: qt c++11 timer qpixmap


    【解决方案1】:

    我在你的代码中发现了一些东西..

    1st,计时器正在工作,但您没有收到通知,因为您的插槽 update() 未在标头中定义为插槽,而是作为方法,

    另一方面,您的代码在我看来不完整,您也需要实现 on_pushButton_clicked 插槽...

    解决这个问题,计时器就会被调用,但就像信息一样,你不需要再次设置间隔

    timer->setInterval(timerInterval);
    

    每次计时器超时...该值根本没有改变,因为您在构造函数中设置了它...

    【讨论】:

    • 感谢您的回复!我是这个ide的新手,很多东西不熟悉。非常感谢您的帮助!
    • 不客气...只要让我们知道这个答案是否解决了问题@yeo.j
    • @yeo.j 很高兴阅读!不要忘记接受我的帖子作为答案,以便以后有相同问题的其他开发人员可以看到原因和解决方案... :-)
    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多