【问题标题】:QLabel show images like a videoQLabel 像视频一样显示图像
【发布时间】:2019-01-23 08:22:03
【问题描述】:
我尝试让 QLabel 像视频一样显示图像。
我希望它慢慢显示我的图像从 f0000.png 到 f0039.png,以便我可以看到进度。
由于某种原因,我的 for 循环以 50 开头。
当我看到程序运行时,它只显示一张图像,或者变化太快,我看不到进度。
如何解决它,让它像视频一样显示图像。
【问题讨论】:
标签:
c++
qt5
qimage
qlabel
【解决方案1】:
您可以使用 Qtimer 并根据需要将速度设置为更快
标题:
#include <QMainWindow>
#include <QTimer>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow() override;
public slots:
void updateLabel();
private:
Ui::MainWindow *ui;
QTimer* _timer;
int index{0};
QString pixResource{};
};
和实现。
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
_timer = new QTimer(parent);
connect(_timer, SIGNAL(timeout()), this, SLOT(updateLabel()));
_timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
_timer->stop();
delete _timer;
}
void MainWindow::updateLabel()
{
if (index >= 10)
{
index = 0;
}
qDebug() << "index: " << index;
pixResource = "res/foo/image/" + QString::number(index) + ".png";
qDebug() << "now the res: " << pixResource;
index++;
}