QSplashScreen 消失得太快
QSplashScreen disappears too quickly
我试图在启动我的应用程序之前显示 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();
}
编辑 使用 official documentation 中的 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();
}
编辑 2 使用 class:
#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()));
或更高数字,实际上没有区别,所以我不确定为什么这不是一个选项。
我也遇到了 this source and I also followed official documentation 但这也没有帮助我找出问题所在。
另外 this post 表明问题一直与 QTimer
有关,但我不明白为什么更大或更小的间隔似乎不算数。
我错过了什么让 QSplashScreen
停留 2 秒而不是立即消失(甚至看不到)?
感谢您指出正确的方向。
问题是,您的代码正在启动计时器然后继续 运行。因此 MainWindow 已创建并显示,SplashScreen 为 deleted/finished。定时器的超时功能将在这一切发生后触发。这就是为什么它这么快关闭的原因。
如果应用程序启动很慢,通常会显示启动画面,因为后台有太多事情要做。在你的情况下,基本上没有负载,代码执行得非常快。
您可以在调用 splash->show()
后立即使用 2 秒的休眠调用,或者放置所有用于关闭 SplashScreen 的代码,在计时器的超时槽中显示 Mainwindow 并删除那里的 SplashScreen。
我试图在启动我的应用程序之前显示 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();
}
编辑 使用 official documentation 中的 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();
}
编辑 2 使用 class:
#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()));
或更高数字,实际上没有区别,所以我不确定为什么这不是一个选项。
我也遇到了 this source and I also followed official documentation 但这也没有帮助我找出问题所在。
另外 this post 表明问题一直与 QTimer
有关,但我不明白为什么更大或更小的间隔似乎不算数。
我错过了什么让 QSplashScreen
停留 2 秒而不是立即消失(甚至看不到)?
感谢您指出正确的方向。
问题是,您的代码正在启动计时器然后继续 运行。因此 MainWindow 已创建并显示,SplashScreen 为 deleted/finished。定时器的超时功能将在这一切发生后触发。这就是为什么它这么快关闭的原因。
如果应用程序启动很慢,通常会显示启动画面,因为后台有太多事情要做。在你的情况下,基本上没有负载,代码执行得非常快。
您可以在调用 splash->show()
后立即使用 2 秒的休眠调用,或者放置所有用于关闭 SplashScreen 的代码,在计时器的超时槽中显示 Mainwindow 并删除那里的 SplashScreen。