Qt - 创建 Main Window 之前的非模态对话框

Qt - Non Modal Dialog before Main Window is created

我一直在努力做到这一点:我想在创建 MainWindow 之前在 window 上显示 QWidget 或 QDialog,但我不能使用 exec() 因为它将进入其循环并且在我接受或拒绝对话框之前不会创建我的 MainWindow。

我想这样做的原因是让小部件在 MainWindow 构造自身时显示信息。一旦 MainWindow 出现,我不想保留这个额外的 window。

我认为问题出在调用 a.exec() 时主 window 已经创建并且 window 不会在 [=39 之前出现=]()。我找到的解决方案是改用 QDialog 并调用 exec() 但它会阻止我不想发生的其余代码。

代码:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StartUpDialog start; //this is my custom QDialog, can be a QWidget if necessary.
qDebug() << "starting up!";
MainWindow w;
start.exec(); //I tried show() but it won't show up.
w.startApp(&start); //this function will do some stuff.
w.show();
//I don't want start to stay after mainwindow shows up
return a.exec();
}

这是我到目前为止尝试过的方法:

希望你能帮助我解决这个问题,感谢阅读!

更新:多亏了 Trap 解决了,我是这样做的:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StartUpDialog start;
QSplashScreen *splash = new QSplashScreen();
StartUpWidget *start = new StartUpWidget(splash);
splash->resize(350,380);
start->show();
splash->raise();
splash->show();
qDebug() << "starting up!";
MainWindow w;
w.startApp(start);
w.show();
splash->finish(&w);
start->deleteLater();
splash->deleteLater();
return a.exec();
 }

我唯一担心的是,我使用 QMovie 在我的小部件中使用了 Gif,显然必须手动更新它。

如果我正确理解你的问题(在你的主 window 创建之前显示一个对话框),你应该看看 QSplashScreen class :http://doc.qt.io/qt-5/qsplashscreen.html