QT专题(一)父窗口、子窗口之间的数据传递

一、主窗口和子窗口设计:

功能:点击Enter Child显示子窗口Dialog1,主窗口输入文字点击Emit可将文字更新到子窗口Dialog1中

主窗口输入文字点击Emit,可直接将文字更新到子窗口Dialog2中,dialog2会自动弹出并显示主界面的文字(注意dialog1和dialog2是两个不同的实例)

QT专题(一)父窗口、子窗口之间的数据传递 QT专题(一)父窗口、子窗口之间的数据传递

 

 

 二、代码:

实现思路:

01主窗口数值传递子窗口:主窗口发出信号,子窗口定义槽函数,主窗口创建connect连接

02子窗口传递数值到主窗口:子窗口发出信号,主窗口定义槽函数,主窗口创建connect连接

项目列表:

 QT专题(一)父窗口、子窗口之间的数据传递

 

 

parentWindow.pro

 

 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2021-06-21T22:20:55
 4 #
 5 #-------------------------------------------------
 6 
 7 QT       += core gui
 8 
 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 
11 TARGET = paraentWindow
12 TEMPLATE = app
13 
14 # The following define makes your compiler emit warnings if you use
15 # any feature of Qt which has been marked as deprecated (the exact warnings
16 # depend on your compiler). Please consult the documentation of the
17 # deprecated API in order to know how to port your code away from it.
18 DEFINES += QT_DEPRECATED_WARNINGS
19 
20 # You can also make your code fail to compile if you use deprecated APIs.
21 # In order to do so, uncomment the following line.
22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
24 
25 CONFIG += c++11
26 
27 SOURCES += \
28         main.cpp \
29         mainwindow.cpp \
30     dialog.cpp
31 
32 HEADERS += \
33         mainwindow.h \
34     dialog.h
35 
36 FORMS += \
37         mainwindow.ui \
38     dialog.ui
39 
40 # Default rules for deployment.
41 qnx: target.path = /tmp/$${TARGET}/bin
42 else: unix:!android: target.path = /opt/$${TARGET}/bin
43 !isEmpty(target.path): INSTALLS += target

dialog.h

 1 #ifndef DIALOG_H
 2 #define DIALOG_H
 3 
 4 #include <QDialog>
 5 //#include "mainwindow.h"
 6 
 7 namespace Ui {
 8 class Dialog;
 9 }
10 
11 class Dialog : public QDialog
12 {
13     Q_OBJECT
14 
15 public:
16     explicit Dialog(QWidget *parent = nullptr);
17      ~Dialog();
18 
19 public slots:
20     //void receiveData(QString data);
21 
22 
23 private slots:
24     void on_pushButton_clicked();
25 
26 void receiveData(QString data);
27 signals:
28     void sendData(QString data);
29 
30 private:
31     Ui::Dialog *ui;
32 
33 };
34 
35 #endif // DIALOG_H

mainWindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <dialog.h>
 6 
 7 namespace Ui {
 8 class MainWindow;
 9 }
10 
11 class MainWindow : public QMainWindow
12 {
13     Q_OBJECT
14 
15 public:
16     explicit MainWindow(QWidget *parent = nullptr);
17     ~MainWindow();
18 public:
19 
20 public slots:
21     void receiveData(QString data);//主窗口接收子窗口发来的数据
22         void on_pushButton_Emit_clicked();
23 private slots:
24     void on_pushButton_OpenChiWin_clicked();
25     void creatConnection();
26 signals:
27     void sendData(QString);
28 
29 
30 
31 private:
32     Ui::MainWindow *ui;
33     Dialog *dia1 = new Dialog;//若只写Dialog *dia1;则在编译时候会报错,因为connect中未初始化就在使用
34     Dialog *dia2 = new Dialog;
//34行是否加括号加this是为了表示该类是否在MainWindow基础上创建的,有this则会在销毁mainwindow时自动关闭Dialog,不加this则即使父窗口关闭,主窗口也不关闭


35 }; 36 37 #endif // MAINWINDOW_H

dialog.cpp

 1 #include "dialog.h"
 2 #include "ui_dialog.h"
 3 #include "mainwindow.h"
 4 
 5 Dialog::Dialog(QWidget *parent) :
 6     QDialog(parent),
 7     ui(new Ui::Dialog)
 8 {
 9     ui->setupUi(this);
10 
11 }
12 
13 Dialog::~Dialog()
14 {
15     delete ui;
16 }
17 
18 void Dialog::on_pushButton_clicked()
19 {
20     emit sendData(ui->lineEdit->text());
21 }
22 
23 void Dialog::receiveData(QString data)
24 {
25     ui->lineEdit->setText(data);
26 }

main.cpp

 1 #include "mainwindow.h"
 2 #include <QApplication>
 3 //#include <dialog.h>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9 
10     w.show();
11 
12     return a.exec();
13 }

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent) :
 5     QMainWindow(parent),
 6     ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     creatConnection();//创建连接
10 }
11 
12 MainWindow::~MainWindow()
13 {
14     delete ui;
15 }
16 
17 void MainWindow::on_pushButton_OpenChiWin_clicked()
18 {
19     dia1->show();
20 }
21 
22 void MainWindow::creatConnection()
23 {
24     connect(dia1,SIGNAL(sendData(QString)),this,SLOT(receiveData(QString)));//子窗口发信号,主窗口接收
25     connect(this,SIGNAL(sendData(QString)),dia1,SLOT(receiveData(QString)));//主窗口发信号,子窗口接收
26   connect(this,SIGNAL(sendData(QString)),dia2,SLOT(receiveData(QString)));
27 }
28 
29 void MainWindow::receiveData(QString data)
30 {
31     ui->lineEdit->setText(data);
32 }
33 
34 void MainWindow::on_pushButton_Emit_clicked()
35 {
36     emit sendData(ui->lineEdit->text());
37     dia2->show();
38 }

 

上一篇:qrcodejs2+html2canvas 生成二维码和海报踩坑记录


下一篇:vue中利用自定义指令修改elementUI对话框dialog到顶部的距离