1. 程式人生 > >Qt QPushButton如何顯示動圖

Qt QPushButton如何顯示動圖

思路:設定個定時器,每隔一段時間,更新背景圖片

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qTimer1 = new QTimer(this);
    //QObject::connect(qTimer1,SIGNAL(timeout()),this,SLOT(refreshBtn()));
    QObject::connect(qTimer1,&QTimer::timeout,this,&MainWindow::refreshBtn);
    qTimer1->start(200);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::refreshBtn()
{
    static int count = 1;
    if(count>6)
        count = 1;
    QString szStyle = "QPushButton#moveBtn{border:0px;background-image:url(:/images/zhuanhuan%d.png);}";
    szStyle.replace("%d",QString::number(count));
    ui->moveBtn->setStyleSheet(szStyle);
    count++;
}