QT 制作开机动画

本文介绍如何使用Qt实现启动动画,并确保其稳定显示直至主窗口出现。通过调整事件循环及消息更新,实现平滑过渡。

根据Qt手册翻译:
The most common usage is to show a splash screen before the main widget is displayed on the screen.
在屏幕显示一个窗口部件之前显示一个动画界面是一个通用惯例.
This is illustrated in the following code snippet in which a splash screen is displayed and some initialization tasks are performed before the application’s main window is shown:
在主窗口展示之前执行一些初始化任务,这个任务就是用代码显示一个开机画面.
Qt:手册代码实例如下:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap pixmap(“:/splash.png”);//相对路径
QSplashScreen splash(pixmap);
splash.show();
app.processEvents(); //循环事件

QMainWindow window;
window.show();
splash.finish(&window);
return app.exec();
}

可是执行结果确是,开机动画停留一瞬间就结束了.在往后看,后面的文档.
Since the splash screen is typically displayed before the event loop has started running,
当事件循环开始执行的时候,开机换面会一直显示.
it is necessary to periodically call QApplication::processEvents() to receive the mouse clicks.
It is sometimes useful to update the splash screen with messages,
事件循环对更新开机画面和消息显示是非常有用的.其实他的意思就是想要动画一直保持,就必须让事件一直循环

根据理解文档手册,不断调试.成功代码如下:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash=new QSplashScreen;
splash->setPixmap(QPixmap(“/home/heisai/Screenshot from 2017-05-15 22-19-38.png”))
splash->setGeometry(QRect(400,200,500,350)); //设置动画位置和大小
splash->show();
QDateTime n=QDateTime::currentDateTime();
QDateTime now;
do{
now=QDateTime::currentDateTime();
//a.processEvents();
splash->showMessage(QObject::tr(“正在加载….”), Qt::AlignCenter|Qt::AlignHCenter, Qt::red);//在画面中央显示文字,字体颜色红色,停留3秒
a.processEvents();
}while(n.secsTo(now)<=3);
do{
now=QDateTime::currentDateTime();
//a.processEvents();
splash->showMessage(QObject::tr(“马上成功……….”), Qt::AlignCenter|Qt::AlignHCenter, Qt::white);//三秒过后,在显示2秒的马上成功。
a.processEvents();
}while(n.secsTo(now)<=5);
MainWindow w;
w.show();
splash->finish(&w);
delete splash;
return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值