Qt在第一个窗口关闭时打开另一个窗口

Qt opening another window when first one has closed

本文关键字:窗口 另一个 第一个 Qt      更新时间:2023-10-16

我已经对Java编程有一段时间了......现在我进入了C++和Qt,我对GUI线程(EDT线程)和工作线程有点迷茫我尝试仅在配置窗口关闭时打开应用程序的主窗口。我不想将用于创建主窗口的代码放在配置窗口的"确定"按钮中。我试图使它们成为模态,但主窗口仍然打开.....船尾配置已完成,我仍然需要查看是否有应用程序更新...所以它的东西像

编辑:这是我的主要:

ConfigurationWindow *cw = new ConfigurationWindow();
//if there is no text file - configuration
cw->show();
//**I need to stop here until user fills the configuration
MainWindow *mw = new MainWindow();
ApplicationUpdateThread *t = new ApplicationUpdateThread();
//connect app update thread with main window and starts it
mw->show();

尝试这样的事情:

#include <QtGui>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDialog *dialog = new QDialog;
    QSlider *slider = new QSlider(dialog);
    QHBoxLayout *layout = new QHBoxLayout(dialog);
    layout->addWidget(slider);
    dialog->setLayout(layout);
    dialog->exec();
    qDebug() << slider->value(); // prints the slider's value when dialog is closed
    QMainWindow mw; // in your version this could be MainWindow mw(slider->value());
    w.show();
    return a.exec();
}

这个想法是主窗口的构造函数可以接受来自QDialog的参数。在这个人为的示例中,我只是使用 qDebug() 在 QDialog 关闭时打印滑块的值,而不是将其作为参数传递,但你明白了。

编辑:您可能还希望在创建主窗口之前"删除"对话框以节省内存。在这种情况下,您需要在删除对话框之前将主窗口构造函数的参数存储为单独的变量。

你必须了解信号和插槽。基本思想是,您将在配置完成后发送信号。你把你的QMainWindow放在一个成员变量中,并在与configurationDone信号连接的主程序的一个插槽中调用mw->show()。

如果你的 ConfigurationWindow 是一个 QDialog,你可以将 finish(int) 信号连接到 MainWindow 的 show() 插槽(并省略 main 的 show() 调用)。