5407
5407

Reputation: 39

QLabel show images like a video

I try to let a QLabel show images like a video.

I want this display my images from f0000.png to f0039.png slowly so I can see progress.

For some reason my for loop starts with 50.

When I see the program run it only show one image or it change too fast I can't see the progress.

How to fix it let it show images like video.

Upvotes: 2

Views: 102

Answers (1)

you can use a Qtimer and set the speed as faster as you need

header:

#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{};
};

and impl.

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++;
}

Upvotes: 2

Related Questions