QMdiArea插槽和信号

时间:2019-02-18 14:29:00

标签: c++ qt qmdiarea

有两个带有窗体的类,从主窗口(MainWindow)中,我在出现的窗体上按下按钮来调用窗体(Dialog),并且在mdiArea的MainWindow中绘制了小部件。问题是出现了对话框,但是当您单击MainWindow中的小部件按钮时,它不会绘制。

MainWindow.h

 #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QGridLayout>
    #include <QLabel>
    #include "QMdiArea"
    #include "QtGui"
    #include "dialog.h"
    #include "form.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow {
    Q_OBJECT

    public:

    explicit MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

    public slots:

    void goToFormWindow();
    void receiverData();

    private:

    Ui::MainWindow* ui;
    Dialog dialog;
    QMdiArea* mdiArea;
    };

    #endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

   connect(ui->pushButton_formWindow, &QPushButton::clicked,   this, &MainWindow::goToFormWindow);
   connect(&dialog,                   &Dialog::goToMainWindow, this, &MainWindow::receiverData);
}

MainWindow::~MainWindow() {
   delete ui;
}

void MainWindow::goToFormWindow() {
   Dialog* dialog = new Dialog();

   dialog->show();
}

void MainWindow::receiverData() {
   QWidget* widget         = new QWidget(mdiArea);
   QGridLayout* gridLayout = new QGridLayout(widget);

   widget->setLayout(gridLayout);
   QLabel* label = new QLabel("Hello, I am sub window!!!", widget);
   gridLayout->addWidget(label);

   mdiArea->addSubWindow(widget);
   widget->setWindowTitle("Sub Window");
   widget->show();
}

Dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include "QtGui"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog {
   Q_OBJECT

public:

   explicit Dialog(QWidget* parent = nullptr);
   ~Dialog();

public slots:

   void onPushButtonAddWindow();

signals:

   void goToMainWindow();

private:

   Ui::Dialog* ui;
};

#endif // DIALOG_H

Dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget* parent) :
   QDialog(parent),
   ui(new Ui::Dialog) {
   ui->setupUi(this);

   connect(ui->pushButton_addWindow, &QPushButton::clicked, this, &Dialog::onPushButtonAddWindow);
}

Dialog::~Dialog() {
   delete ui;
}

void Dialog::onPushButtonAddWindow() {
   emit goToMainWindow();
}

0 个答案:

没有答案