QT 制作开机动画

来源:互联网 发布:日韩护肤品推荐 知乎 编辑:程序博客网 时间:2024/06/10 02:49

根据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();
}

原创粉丝点击