Qt在定时器上改变图片
Qt change picture on timer
我正在尝试在 Qt 中更改 QLabel 上的图像。
一开始,我做了基本的设置图如下:
void MainWindow::setMainDisplayNew(QString imageName){
QPixmap pix7 = imageName;
QPixmap pix8 = pix7.scaled(QSize(720,480), Qt::KeepAspectRatio);
ui->mainDisplay->setStyleSheet(imageName);
ui->mainDisplay->setPixmap(pix8);
}
现在我想更改它以便我可以传递 2 个数组。图像列表和它们应该出现的持续时间,我希望显示器在指定的持续时间内显示它们。
void MainWindow::setMainDisplay(QString imageName[], int size)
{
for(unsigned int i=0; i<size; i++)
{
QTimer * timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=](){
setMainDisplayNew(imageName[i]);
timer->deleteLater(); // ensure we cleanup the timer
});
timer->start(3000);
}
}
编辑
在回复的帮助下,我得到了上面的代码。我正在发送 3 张图片。它会在 3 秒后显示最终图像并保持原样...有帮助吗?
while(true){
这是你的问题。 Qt 是一个event-driven framework。 while(true) 阻止事件被处理。此类事件包括 QTimer 超时和 GUI 更新。您需要允许该函数退出。
此外,您没有清理计时器,因此每次进入函数时都会泄漏内存(尽管目前只有一次!)。您可以通过调用 deleteLater 进行清理。
使用C++ 11,会是这样的:-
for(int i=0; i<mySize; i++)
{
QTimer * timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=](){
setMainDisplayNew(imageName[i]);
timer->deleteLater(); // ensure we cleanup the timer
});
timer->start(duration[i]);
}
我正在尝试在 Qt 中更改 QLabel 上的图像。
一开始,我做了基本的设置图如下:
void MainWindow::setMainDisplayNew(QString imageName){
QPixmap pix7 = imageName;
QPixmap pix8 = pix7.scaled(QSize(720,480), Qt::KeepAspectRatio);
ui->mainDisplay->setStyleSheet(imageName);
ui->mainDisplay->setPixmap(pix8);
}
现在我想更改它以便我可以传递 2 个数组。图像列表和它们应该出现的持续时间,我希望显示器在指定的持续时间内显示它们。
void MainWindow::setMainDisplay(QString imageName[], int size)
{
for(unsigned int i=0; i<size; i++)
{
QTimer * timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=](){
setMainDisplayNew(imageName[i]);
timer->deleteLater(); // ensure we cleanup the timer
});
timer->start(3000);
}
}
编辑
在回复的帮助下,我得到了上面的代码。我正在发送 3 张图片。它会在 3 秒后显示最终图像并保持原样...有帮助吗?
while(true){
这是你的问题。 Qt 是一个event-driven framework。 while(true) 阻止事件被处理。此类事件包括 QTimer 超时和 GUI 更新。您需要允许该函数退出。
此外,您没有清理计时器,因此每次进入函数时都会泄漏内存(尽管目前只有一次!)。您可以通过调用 deleteLater 进行清理。
使用C++ 11,会是这样的:-
for(int i=0; i<mySize; i++)
{
QTimer * timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=](){
setMainDisplayNew(imageName[i]);
timer->deleteLater(); // ensure we cleanup the timer
});
timer->start(duration[i]);
}