【问题标题】:pause function while child window is displayed in Qt在 Qt 中显示子窗口时暂停功能
【发布时间】:2012-07-20 12:37:31
【问题描述】:

在 Qt Creator 中,我有一个主窗口和一个 QWidget 作为工具窗口 (setWindowFlags(Qt::tool))。当我调用工具窗口时,用户可以更改一些设置。这些更改随后会更改主窗口中的一些数据。

我创建了小部件,显示它,然后我想更新主窗口中的数据,但该功能不等待小部件关闭。所以演出后的更新程序立即执行,没有任何效果。当我显示 QMessageBox 时,该函数会等待用户关闭它。

我可以为 QWidget 设置一个标志或其他东西,以便函数等待吗?

void userclicksonsettings(){
 settings = new Settings(this);  // Settings is a QWidget-class with ui
 settings->show();
 // function should wait till settings is closed
 // set up mainwindow with new values
}

谢谢。

【问题讨论】:

    标签: qt window show


    【解决方案1】:

    我刚刚解决了。 使用 QDialog 而不是 QWidget 作为基类允许调用窗口 QDialog::exec(); 并且父窗口小部件将暂停,直到窗口再次关闭。

    编辑:这是我刚刚从备份磁盘中挖掘出来的解决方案的来源。我不得不说,这是几年前我最后一次使用 Qt 和这段代码,所以它可能是不正确的。我希望它有助于理解这个想法。

    settingsForm.h

    #include <QDialog>
    class SettingsForm : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit SettingsForm(QWidget *parent = 0);
        ~SettingsForm();
    // other variables and slots etc.
    };
    

    settingsForm.cpp

    #include "settingsform.h"
    #include "ui_settingsForm.h"
    
    #include <QColorDialog>
    
    SettingsForm::SettingsForm(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::SettingsForm)
    {
        ui->setupUi(this);
        this->setWindowFlags(Qt::Tool);
    
    // initializing functions
    }
    
    SettingsForm::~SettingsForm()
    {
        delete ui;
    }
    

    主窗口.h

    #include "settingsForm.h"
    // ...
    

    从主窗口调用settingsWindow初始化对象并像QDialog一样调用它

    主窗口.cpp

    settingsform = new SettingsForm(this);
    if(settingsform->exec() == QDialog::Accepted){
        // update form from settings
    }
    

    我还有一个设置类,用于可以使用表单设置的所有变量,它被传递给 settingsForm 并在用户单击“确定”时更新。

    【讨论】:

      【解决方案2】:

      我是这样做的:

      void userclicksonsettings() {
          settings = new Settings(this);
          settings->setWindowModality(Qt::ApplicationModal);
          settings->show();
         //...
      }
      

      Qt::ApplicationModal - 该窗口对应用程序是模态的,并阻止所有窗口的输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-16
        • 1970-01-01
        • 1970-01-01
        • 2017-01-08
        相关资源
        最近更新 更多