【问题标题】:Qt opening another window when first one has closedQt在第一个窗口关闭时打开另一个窗口
【发布时间】:2012-04-12 02:05:42
【问题描述】:

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

编辑:这是我的主要内容:

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();

【问题讨论】:

  • 您是专门指应用程序的启动吗?另外,您将发布的代码放在哪里?在 main() 中?

标签: c++ multithreading qt window


【解决方案1】:

试试这样的:

#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 关闭时打印滑块的值,而不是将其作为参数传递,但你明白了。

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

【讨论】:

  • +1 我打算投反对票,然后我意识到这正是 OP 要求的:)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多