首页 > 解决方案 > 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

我缺少什么来保持QSplashScreen2 秒钟而不是立即消失(甚至看不到它)?感谢您指出正确的方向。

标签: qtc++11qt5qtimer

解决方案


问题是,您的代码正在启动计时器,然后继续运行。因此 MainWindow 被创建、显示并且 SplashScreen 被删除/完成。定时器的超时功能将在这一切发生后触发。这就是为什么它关闭得如此之快。

如果应用程序启动很慢,通常会显示 SplashScreens,因为后台发生了很多事情。在您的情况下,基本上没有负载,并且代码执行得非常快。

您可以在调用后立即使用睡眠调用 2 秒,也可以splash->show()将所有代码用于关闭 SplashScreen,在计时器的超时槽中显示主窗口并删除那里的 SplashScreen。


推荐阅读