一.前言
楼主在实际开发中,会遇到一些情况就是软件启动的时候会去登录服务器,获取一些信息,这个过程中会有一定的耗时,如果一开始就出现软件界面,然后没有反应,客户体验感会变差,所以可以设置一个开机效果来作为缓冲。楼主分享两类遇到的需要增加动画
二.实现过程
备注:以下代码都在main.cpp中添加
1.处理完耗时的操作再显示界面
Widget w;
QPixmap pixmap("://loading.png");
QSplashScreen *sp = new QSplashScreen(pixmap);
// showMessage参数分别为 提示文本,提示文本位置(相对图片) 文本颜色
sp->showMessage("软件正在疯狂加载中",Qt::AlignCenter,Qt::red);
sp->show();
// 不断重绘制
sp->repaint();
// 模拟登录
w.isLogin();
sp->finish(&w);
delete sp;
sp = nullptr;
w.show();
return a.exec();
void Widget::isLogin()
{
// 模拟登录操作耗时
for(int i=0;i<10000;i++)
{
//
}
}
2.做固定的延时再显示界面
Widget w;
QPixmap pixmap("://loading.png");
QSplashScreen *sp = new QSplashScreen(pixmap);
// showMessage参数分别为 提示文本,提示文本位置(相对图片) 文本颜色
// sp->showMessage("软件正在疯狂加载中",Qt::AlignCenter,Qt::red);
sp->show();
QDateTime time=QDateTime::currentDateTime();
QDateTime curTime = QDateTime::currentDateTime();
qDebug()<<time.secsTo(curTime);
while (time.secsTo(curTime)<=5) {
QString str = QString("%1:%2").arg("软件正在疯狂加载中").arg(QString::number(time.secsTo(curTime)));
sp->showMessage(str,Qt::AlignCenter,Qt::red);
curTime=QDateTime::currentDateTime();
a.processEvents();
}
// 不断重绘制
// sp->repaint();
// 模拟登录
//w.isLogin();
w.show();
sp->finish(&w);
delete sp;
sp = nullptr;
return a.exec();