QT利用QMovie实现动态加载、等待提示框
立即下载
资源介绍:
有时候要进行某项后台处理,但后台处理时间很长,需要提示用户等待,这时候做一个动态等待窗口,可以清晰展示当前操作的状态,以便用户能够理解和接受。本实例实现方式为获取一张gif的资源图,然后使用QMovie 在一个QLabel 控件上加载显示gif的waiting等待动态。
#include "loadingdialog.h"
#include
LoadingDialog::LoadingDialog(QWidget *parent) : QDialog(parent)
{
m_pParent = parent;
//如果需要显示任务栏对话框则删除Qt::Tool
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground, true);
init();
}
LoadingDialog::~LoadingDialog()
{
delete m_lable;
delete m_movie;
delete m_centerFrame;
deleteLater();
}
void LoadingDialog::init()
{
this->setFixedSize(100, 100);
m_centerFrame = new QFrame(this);
m_centerFrame->setGeometry(10, 10 ,this->width()-10, this->height()-10);
//加载Loading动画
m_lable = new QLabel();
m_movie = new QMovie(":/res/loading.gif");
int size = qMin(m_lable->width(),m_lable->height());
m_movie->setScaledSize(QSize(size, size));
m_lable->setScaledContents(true);
m_lable->setMovie(m_movie);
m_movie->start();
QGridLayout *gridLayout = new QGridLayout();
gridLayout->setSpacing(0);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setContentsMargins(2, 2, 2, 2);
gridLayout->addWidget(m_lable);
m_centerFrame->setLayout(gridLayout);
m_pTimer = new QTimer(this);
connect(m_pTimer, &QTimer::timeout, this, &LoadingDialog::onTimerTimeout, Qt::UniqueConnection);
m_pTimer->start(10);
}
void LoadingDialog::move_to_center(QWidget *pParent)
{
m_pParent = pParent;
if(pParent != nullptr && pParent != NULL)
{
int nParentWidth = pParent->width();
int nParentHeigth = pParent->height();
int nWidth = this->width();
int nHeight = this->height();
int nParentX = pParent->x();
int nParentY = pParent->y();
int x = (nParentX + (nParentWidth - nWidth) / 2);
int y = (nParentY + (nParentHeigth - nHeight) / 2);
this->move(x, y);
}
}
void LoadingDialog::onTimerTimeout()
{
move_to_center(m_pParent);
}