Qt 子窗口父窗口切换,窗口间传值

来源:互联网 发布:淘宝有哪些正品鞋店 编辑:程序博客网 时间:2024/05/23 20:02

 本人代码

代码下载:http://download.csdn.net/detail/u013378306/9621322

效果:实现 父窗口 打开子窗口,关闭子窗口,子窗口向父窗口传值


win_win.pro 文件
#-------------------------------------------------## Project created by QtCreator 2016-09-04T13:33:26##-------------------------------------------------QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = win_winTEMPLATE = appSOURCES += main.cpp\        dialog.cpp \    mainwindow.cppHEADERS  += dialog.h \    mainwindow.hFORMS    += dialog.ui \    mainwindow.ui


父窗口 mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include "dialog.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();     Dialog *dlg;private:    Ui::MainWindow *ui;private slots:    void receiveData(QString data);    void on_pushButton_clicked();    void on_pushButton_2_clicked();};#endif // MAINWINDOW_H


子窗口 dialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>namespace Ui {class Dialog;}class Dialog : public QDialog{    Q_OBJECTpublic:    explicit Dialog(QWidget *parent = 0);    ~Dialog();private:    Ui::Dialog *ui;signals:    void  sendData(QString);private slots:    void buttonBox_accepted();};#endif // DIALOG_H



父窗口 mainwindow.cpp
#include "mainwindow.h"#include "ui_mainwindow.h"#include "dialog.h"MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);       //信号槽方式下父子窗体传值的测试      MainWindow::dlg = new Dialog;       //关联信号和槽函数       connect(dlg,SIGNAL(sendData(QString)),this,SLOT(receiveData(QString)));      // dlg->setModal(true); 不论是模态或者非模态都可以正常传值       //dlg->show();}MainWindow::~MainWindow(){    delete ui;}/**  *接收子窗口消息 * @brief MainWindow::receiveData * @param data */void MainWindow::receiveData(QString data){   this->show();    ui->textBrowser_1->setText(data);}/**关闭子窗口,使用界面方式添加的槽,代码中没有connect * @brief MainWindow::on_pushButton_clicked */void MainWindow::on_pushButton_clicked(){  dlg->close();}/**显示子窗口 * @brief MainWindow::on_pushButton_2_clicked */void MainWindow::on_pushButton_2_clicked(){    dlg->show();}


子窗口 dialog.cpp
#include "dialog.h"#include "ui_dialog.h"#include "mainwindow.h"Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    //MainWindow *m=new MainWindow;    ui->setupUi(this);    connect(ui->pushButton_1,SIGNAL(clicked()),this,SLOT(buttonBox_accepted()));}Dialog::~Dialog(){    delete ui;}/**向父窗口发送信息 * @brief Dialog::buttonBox_accepted */void Dialog::buttonBox_accepted(){    emit sendData(ui->lineEdit->text());}

主程序mian.cpp
#include "dialog.h"#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){    QApplication a(argc, argv);    //Dialog w;   // w.show();    MainWindow b;    b.show();    return a.exec();}



父窗口 mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow">  <property name="geometry">   <rect>    <x>0</x>    <y>0</y>    <width>800</width>    <height>600</height>   </rect>  </property>  <property name="windowTitle">   <string>MainWindow</string>  </property>  <widget class="QWidget" name="centralwidget">   <widget class="QTextBrowser" name="textBrowser_1">    <property name="geometry">     <rect>      <x>70</x>      <y>60</y>      <width>256</width>      <height>192</height>     </rect>    </property>   </widget>   <widget class="QPushButton" name="pushButton">    <property name="geometry">     <rect>      <x>470</x>      <y>90</y>      <width>75</width>      <height>23</height>     </rect>    </property>    <property name="text">     <string>关闭子窗口</string>    </property>   </widget>   <widget class="QPushButton" name="pushButton_2">    <property name="geometry">     <rect>      <x>470</x>      <y>140</y>      <width>75</width>      <height>23</height>     </rect>    </property>    <property name="text">     <string>显示子窗口</string>    </property>   </widget>   <widget class="QLabel" name="label">    <property name="geometry">     <rect>      <x>130</x>      <y>20</y>      <width>91</width>      <height>16</height>     </rect>    </property>    <property name="text">     <string>接收子窗口值</string>    </property>   </widget>  </widget>  <widget class="QMenuBar" name="menubar">   <property name="geometry">    <rect>     <x>0</x>     <y>0</y>     <width>800</width>     <height>23</height>    </rect>   </property>   <widget class="QMenu" name="menu">    <property name="title">     <string>父窗口</string>    </property>   </widget>   <addaction name="menu"/>  </widget>  <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>


子窗口 dialog.ui

<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>Dialog</class> <widget class="QDialog" name="Dialog">  <property name="geometry">   <rect>    <x>0</x>    <y>0</y>    <width>400</width>    <height>300</height>   </rect>  </property>  <property name="windowTitle">   <string>Dialog</string>  </property>  <widget class="QLineEdit" name="lineEdit">   <property name="geometry">    <rect>     <x>60</x>     <y>50</y>     <width>113</width>     <height>20</height>    </rect>   </property>  </widget>  <widget class="QPushButton" name="pushButton_1">   <property name="geometry">    <rect>     <x>250</x>     <y>50</y>     <width>75</width>     <height>23</height>    </rect>   </property>   <property name="text">    <string>向父窗口传值</string>   </property>  </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/></ui>





网上列子

在写程序时,难免会碰到多窗体之间进行传值的问题。依照自己的理解,我把多窗体传值的可以使用的方法归纳如下:

      1.使用QT中的Signal&Slot机制进行传值;

      2.使用全局变量;

      3.使用public形式的函数接口;

      4.使用QT中的Event机制(这种没有把握,但是感觉应该是可以的),但是实现起来应该比前几种复杂,这里不做讨论。

       下面分别进行讨论:

1.使用QT中的Signal&Slot机制进行传值:

      QT中的Signal&Slot机制相比于MFC中的消息机制简单了许多,它保证了任何对象之间均可以通过这种方式进行通信,甚至可以得到消息的sender。这里就拿一个简单的窗体间传值作为例子。

      首先看一下主窗体MainWindow:

      在设计器中拖拽一个Label和一个TextEdit控件到界面上,TextEdit用于显示传递过来的数据。

    创建一个右下有两个按键的对话框,放置一个Label和一个LineEdit。

     下面就是编码的操作了,我们需要在Dialog中声明一个信号,当用户点击OK时传递LineEdit中的内容到mainWindow中,具体的dialog.h代码为:

[cpp] view plain copy
  1. #ifndef DIALOG_H  
  2. #define DIALOG_H  
  3.   
  4. #include <QDialog>  
  5.   
  6. namespace Ui {  
  7. class Dialog;  
  8. }  
  9.   
  10. class Dialog : public QDialog  
  11. {  
  12.     Q_OBJECT  
  13.       
  14. public:  
  15.     explicit Dialog(QWidget *parent = 0);  
  16.     ~Dialog();  
  17.       
  18. private:  
  19.     Ui::Dialog *ui;  
  20. signals:  
  21.     void  sendData(QString);  
  22. private slots:  
  23.     void on_buttonBox_accepted();  
  24. };  
  25.   
  26. #endif // DIALOG_H   
      其中的signals:void sendData(QString)便是我们需要的信号函数,同时声明了一个槽函数

       void on_buttonBox_accepted();用于相应确定按钮的click事件。下面就是需要在该函数中产生一个信号。代码如下:

[cpp] view plain copy
  1. void Dialog::on_buttonBox_accepted()  
  2. {  
  3.     emit sendData(ui->lineEdit->text());  
  4. }  
      代码异乎寻常的简单,只需要用emit的方式调用sendData函数,将需要的参数传递进去即可。而MainWindow中则需要声明接收的槽函数,注意槽函数参数只能与信号函数少或相等,而不能多于信号函数参数个数。在MainWindow的头文件中声明槽函数:

[cpp] view plain copy
  1. private slots:  
  2.     void receiveData(QString data);  

    为了便于测试,我只在MainWindow的构造函数中创建了一个Dialog对象,并连接了信号和槽,具体为:

[cpp] view plain copy
  1. MainWindow::MainWindow(QWidget *parent) :  
  2.     QMainWindow(parent),  
  3.     ui(new Ui::MainWindow)  
  4. {  
  5.     ui->setupUi(this);  
  6.     //信号槽方式下父子窗体传值的测试  
  7.     Dialog *dlg = new Dialog;  
  8.     //关联信号和槽函数  
  9.     connect(dlg,SIGNAL(sendData(QString)),this,SLOT(receiveData(QString)));  
  10.    // dlg->setModal(true); 不论是模态或者非模态都可以正常传值  
  11.     dlg->show();  
  12. }  
      这里,我没有将父窗口的指针传递到Dialog中,如new Dialog(this),这种方式下,实际上可以归结到第三类传值方式中去。因为此时,可以使用MainWindow中的父窗口的函数进行数据的赋值和操作。

      这里,可能还有一个问题就是,父窗口如何给子窗口传值,一方面,仍然可以使用信号和槽的方式进行,但是,我感觉更便利的方式倒是使用这种public接口的方式进行传值。这种来的更直接和明显。当然,可以看出Signal&Signal方式进行此类的处理会更有通用性。

    在receiveData(QString)的槽函数中进行接收到数据的处理,这里仅仅进行了简单的显示:

[html] view plain copy
  1. void MainWindow::receiveData(QString data)  
  2. {  
  3.     ui->textEdit->setText(data);  
  4. }  

    最后看下结果:

  

       最终的结果,因为信号和槽可以是多对多的,所以,在类似多个窗体广播信息时,这种方式就很有用,当然也可以使用全局变量的形式。

      

0 0