【问题标题】:How to launch Mainwindow from a dialog in Qt如何从 Qt 中的对话框启动主窗口
【发布时间】:2021-02-03 12:07:16
【问题描述】:

我想从对话框中打开我的应用程序的主窗口。实际上我想先启动一个小对话框窗口,然后在对话框上按确定按钮后,主窗口应该打开。我现在的方法如下所示,但它不起作用。

在主窗口加载以下行

Dialog diag;
diag.setModal(true);
diag.exec()

在对话框的 OK 按钮的单击事件中,我输入了以下几行:

Mainwindow mainw;
mainw.show()

请建议如何正确执行此操作,以便通过单击首先出现的对话框上的确定按钮打开主窗口。

完整代码如下 以下是@Farshid616 建议的我的完整代码

主窗口.ccp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
           
        Dialog *dialog=new Dialog();
    
        ui->setupUi(this);
    
      

QObject::connect(dialog,&Dialog::accepted,this,&MainWindow::onAccept);
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }


void MainWindow::onAccept()
{
    this->show();
}

ma​​inwindow.h

private slots:     
    void onAccept();
  

dialog.h

signals:
    void accepted();

dialog.cpp

 #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_buttonBox_accepted()
    {
        emit accepted();
        this->accept();
    } 

mainwindow.ui 没有小部件,而 dialog.ui 有带有确定和取消按钮的 buttonBox。

【问题讨论】:

    标签: windows qt


    【解决方案1】:

    您应该在对话框类中创建一个信号,并将 MainWindow 类上的该信号连接到一个插槽并在该插槽上显示您的 MainWindow。

    MainWindow.cpp

    MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    
        ui->setupUi(this);
    
        Dialog *dialog=new Dialog();
        QObject::connect(dialog,&Dialog::accepted,this,&MainWindow::onAccept);
        dialog->show();
    }
    
    void MainWindow::onAccept()
    {
        this->show();
    }
    

    ma​​inwindow.h

    private slots:
        void onAccept();
    

    dialog.h

    signals:
        void accepted();
    

    dialog.cpp

    void Dialog::on_buttonBox_accepted()
    {
        emit accepted();
        this->accept();
    }
    

    不要忘记删除 main.cpp 文件中的 w.show(); 行。

    【讨论】:

    • 如何在对话框类中创建信号请分享代码
    • 我已经更新了答案,请查看。另请阅读此答案stackoverflow.com/questions/42714173/…
    • 我试图实现你建议的代码。它给出了错误没有匹配的连接调用(.....)。我已将对话框定义为 Dialog 的实例。请提出解决方案。
    • 你应该在 MainWindow 构造函数中编写连接
    • 我已使用您建议的代码编辑了我的原始帖子。它运行但主窗口首先打开而不是对话框。请您发表评论。
    【解决方案2】:

    你应该在函数作用域之外创建一个MainWindow对象,否则它会在函数结束时被销毁。

    #include "mainwindow.h"
    
    #include <QApplication>
    #include <QDialog>
    #include <QVBoxLayout>
    #include <QLabel>
    #include <QPushButton>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QDialog dialog;
        QPushButton *accept_button = new QPushButton{"Accept"};
        QObject::connect(accept_button, &QPushButton::clicked, &dialog, &QDialog::accept);
        QPushButton *reject_button = new QPushButton{"Reject"};
        QObject::connect(reject_button, &QPushButton::clicked, &dialog, &QDialog::reject);
        QVBoxLayout *dialog_layout = new QVBoxLayout{&dialog};
        dialog_layout->addWidget(new QLabel{"Open mainwindow?"});
        dialog_layout->addWidget(accept_button);
        dialog_layout->addWidget(reject_button);
    
        MainWindow w;
        if (dialog.exec() == QDialog::Accepted)
        {
            qDebug() << "Accepted";
            w.show();
        }
        else
            qDebug() << "Rejected";
    
        return a.exec();
    }
    

    【讨论】:

      【解决方案3】:

      我最近在我的应用中实现了相同的架构。这就是我的做法。我用的是 Pyside2,你可以把我的代码翻译成 C++。

      class MainWindow(QtWidgets.QMainWindow,  Ui_MainWindow):
      
          def __init__(self, file_name,parent=None):
              super(MainWindow, self).__init__(parent)
              """
              __init__ code lines
              """
      
      
      class HomeDialog(QtWidgets.QDialog, home_dialog.Ui_Dialog):
          def __init__(self, parent=None):
              super(HomeDialog, self).__init__(parent)
              self.setupUi(self)
              self.openFile.clicked.connect(self.get_file_name)
      
          def get_file_name(self):
              file_name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open config file',
                                                                  dir=path.join("/"),
                                                                  filter="xml (*.xml)")
              if not file_name[0]:
                  return None
              else:
                  self.path = file_name
                  self.accept()
      
      if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          app.setStyle(ProxyStyle())
          d = HomeDialog()
          if d.exec_():
              mainWin = MainWindow(file_name=d.path)
              mainWin.show()
              sys.exit(app.exec_())
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        • 2013-02-06
        • 2010-10-15
        • 1970-01-01
        • 2019-01-01
        相关资源
        最近更新 更多