【发布时间】:2020-03-17 12:43:36
【问题描述】:
我试图在启动我的应用程序之前显示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();
}
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())); 甚至更高的数字,实际上没有区别,所以我不确定为什么这不是一个选项。
我还遇到了this source,我也关注了official documentation,但这也无助于我找出问题所在。
另外this post 表明问题一直与QTimer 相关,但我不明白如何,因为更大或更小的间隔似乎不算数。
为了让QSplashScreen 保持 2 秒而不是立即消失(甚至看不到它),我缺少什么?
感谢您指出正确的方向。
【问题讨论】:
-
您是否尝试使用
QSplashScreen::finish()函数而不是计时器? -
你好 vahancho,不,我没有,但我先试一试
-
它没有任何区别。来自here,我应该对示例代码进行哪些修改?
QTimer是否仍然需要或不再需要? -
你根本不需要计时器,IMO。请查看您引用的同一页面以查看使用示例。您应该在
w.show()调用后调用splash->finish(&w);并删除所有计时器。 -
好的,我明白了。是的,它现在可以在没有
QTimers的情况下使用,但图像仍然会很快消失。我怎样才能让它停留 2 秒呢?