是否有办法让Qlabel每十秒钟显示一次不同的图像?
我将超时信号连接到名为“ changePixmap”的插槽上
// under ui->setupUi(this);
QTimer *slideShow = new QTimer(this);
connect(slideShow,SIGNAL(timeout()),this, SLOT(changePixmap()));
slideShow->start(10000);
void MainWindow::changePixmap(){
QString imageNumber = "1";
ui->photoLabel->setScaledContents(true);
ui->photoLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
ui->photoLabel->setPixmap(QPixmap("image" + imageNumber + ".jpg"));
ui->photoLabel->show();
imageNumber = (imageNumber.toInt()+1) % 5;
}
程序运行并将一个图像输出到Qlabel,但不会更改为下一个图像。
答案 0 :(得分:4)
imageNumber是允许您更改图像的变量,但是在您的情况下,它始终为“ 1”,因此图像不会更改,由于更改丢失,最后一行代码无济于事。
解决方案是使imageNumber成为类的成员,也最好是整数。
*。h
// ...
private:
int imageNumber = 0;
*。cpp
void MainWindow::changePixmap(){
ui->photoLabel->setScaledContents(true);
ui->photoLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
ui->photoLabel->setPixmap(QPixmap(QString("image%1.jpg").arg(imageNumber));
ui->photoLabel->show();
imageNumber = (imageNumber + 1) % 5;
}