qt - QSplashScreen 消失得太快
问题描述
我试图QSplashScreen
在启动我的应用程序之前显示一个。我的问题是QSplashScreen
消失得太快了。我希望它在实际应用程序加载之前显示 2 秒。在我为显示错误而构建的小示例下方:
#include <QApplication>
#include <QSplashScreen>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/dredgingSplash.png"));
splash->show();
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white);
QTimer::singleShot(2500, splash, SLOT(close()));
MainWindow w;
QTimer::singleShot(2500, &w, SLOT(show()));
splash->showMessage(QObject::tr("loading modules..."), topRight, Qt::white);
splash->showMessage(QObject::tr("Establishing Connections..."), topRight, Qt::white);
w.show();
delete splash;
return a.exec();
}
使用QSplashScreen::finish()
来自官方文档的编辑:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/dredgingSplash.png"));
splash->show();
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white);
MainWindow w;
splash->showMessage(QObject::tr("loading modules..."), topRight, Qt::white);
splash->showMessage(QObject::tr("Establishing Connections..."), topRight, Qt::white);
w.show();
splash->finish(&w);
return a.exec();
}
EDITS 2使用一个类:
#include <QApplication>
#include <QSplashScreen>
#include <QTimer>
class ShowImageTime {
public:
void slInit();
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/dredgingSplash.png"));
splash->show();
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(QObject::tr("Setting up the main window..."), topRight, Qt::white);
MainWindow w;
QTimer::singleShot(3000, splash, SLOT(close()));// close splash after 4s
QTimer::singleShot(3000, &w, SLOT(ShowImageTime::slInit(this->show)));// mainwindow reappears after 4s
splash->showMessage(QObject::tr("loading modules..."), topRight, Qt::white);
splash->showMessage(QObject::tr("Establishing Connections..."), topRight, Qt::white);
w.show();
splash->finish(&w);
return a.exec();
}
我尝试使用,QTimer
因为我认为这可能是由于加载优先级而导致的潜在问题,但不幸的是,如果我改变QTimer::singleShot(2500, splash, SLOT(close()));
甚至QTimer::singleShot(12500, splash, SLOT(close()));
更高的数字,实际上没有区别,所以我不确定为什么这不是一个选项。
我也遇到了这个来源,我也关注了官方文档,但这也没有帮助我找出问题所在。此外,这篇文章表明问题与QTimer
我缺少什么来保持QSplashScreen
2 秒钟而不是立即消失(甚至看不到它)?感谢您指出正确的方向。
解决方案
问题是,您的代码正在启动计时器,然后继续运行。因此 MainWindow 被创建、显示并且 SplashScreen 被删除/完成。定时器的超时功能将在这一切发生后触发。这就是为什么它关闭得如此之快。
如果应用程序启动很慢,通常会显示 SplashScreens,因为后台发生了很多事情。在您的情况下,基本上没有负载,并且代码执行得非常快。
您可以在调用后立即使用睡眠调用 2 秒,也可以splash->show()
将所有代码用于关闭 SplashScreen,在计时器的超时槽中显示主窗口并删除那里的 SplashScreen。
推荐阅读
- python - 如何在Qtablewidget PYQT中禁用除一列之外的所有列-python
- java - 无法向 Firebase 进行身份验证
- python - 具有列或行最大限制的 Pyspark 数据框
- maven - 如何在 pom.xml 中检索 Maven 内部版本号?
- angular - 如何在Angular 4中使用打字稿基于其他下拉选择值显示下拉值
- c++ - 在 Windows 中运行 .exe 文件时如何防止 ucrtbase.dll 崩溃?
- c - 从文件的 inode 号获取文件名
- dashboard - 如何将现有仪表板复制到 Appdynamics 中的新项目
- python - 在哪里可以找到内置类和 ABC 的 Python 类型提示?
- javascript - Web 组件中页面加载时的模式弹出窗口