Qt make a window waiting to load

As shown in the figure below, a gif image, play it through the code to achieve the waiting effect, you can also add some text below:

Go directly to the code, cpp file:

#include "loadingwidget.h"
#include "ui_loadingwidget.h"

QLoadingWidget::QLoadingWidget(QWidget *parent) :
    QDialog(parent),
    ui (new Ui :: QLoadingWidget)
{
    ui-> setupUi (this);

    setWindowFlags(Qt::FramelessWindowHint);//No border
    setAttribute(Qt::WA_TranslucentBackground);//The background is transparent

    // center the screen
    int frmX = width();
    int frmY = height();

    QDesktopWidget in;
    int deskWidth = w.width();
    int deskHeight = w.height();

    QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
    move(movePoint);

    //load gif image
    QMovie *movie = new QMovie(":/new/prefix1/res/loading");
    ui->lbl_gif->setMovie(movie);
    movie->start();
}

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

.h file:

#include <QDialog>
#include <QDesktopWidget>
#include <QMovie>

namespace Ui {
class QLoadingWidget;
}

class QLoadingWidget : public QDialog
{
    Q_OBJECT

public:
    explicit QLoadingWidget(QWidget *parent = 0);
    ~QLoadingWidget();

private:
    Ui::QLoadingWidget *ui;
};

transfer:

QLoadingWidgetloadingWidget;

loadingWidget.show();

Note that you have to remember to close it after opening it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344850&siteId=291194637