Qt5 UI内嵌UI的实现&相互通信的方法

主要用到QStackedWidget和友元类声明,废话不多说直接上代码。

工程架构:

Qt5 UI内嵌UI的实现&相互通信的方法_第1张图片

1.mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

class Form;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    friend class Form;
    class Form *form;

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

2.mainwindow.ui



 MainWindow
 
  
   
    0
    0
    561
    507
   
  
  
   MainWindow
  
  
   
    
     5
    
    
     5
    
    
     5
    
    
     5
    
    
     5
    
    
     
      
       
        
         Main Text Edit
        
       
      
      
       
        
         ...
        
       
      
     
    
    
     
      
      
     
    
    
     
      
       
        
         Qt::Horizontal
        
        
         
          40
          20
         
        
       
      
      
       
        
         Main Btn
        
       
      
     
    
   
  
  
   
    
     0
     0
     561
     26
    
   
  
  
 
 
 
 

3.mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include "ui_form.h"

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

    ui->stackedWidget->addWidget(form);
    ui->stackedWidget->setWindowTitle("Form");
    ui->stackedWidget->setCurrentWidget(form);
}

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

void MainWindow::on_pushButton_clicked()
{
    form->ui->lineEdit->setText("99999999");
}
4.form.h
#ifndef FORM_H
#define FORM_H

#include 

class MainWindow;

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = 0);
    ~Form();
    friend class MainWindow;
    class MainWindow *mainUi;
    void addText();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Form *ui;
};

#endif // FORM_H

5.form.cpp

#include "form.h"
#include "ui_form.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

void Form::on_pushButton_clicked()
{
    mainUi->ui->lineEdit->setText("0000000");
}
6.form.ui


 Form
 
  
   
    0
    0
    890
    671
   
  
  
   Form
  
  
   
    
     
      
       
        From Text Edit
       
      
     
     
      
       
        ...
       
      
     
    
   
   
    
   
   
    
     
      
     
     
      
       
        ...
       
      
     
    
   
   
    
     
      
       
        Qt::Horizontal
       
       
        
         40
         20
        
       
      
     
     
      
       
        From Btn
       
      
     
    
   
  
 
 
 

7.main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

8.UiInUiTest.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-07-01T13:51:03
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = UiInUiTest
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp \
    form.cpp

HEADERS += \
        mainwindow.h \
    form.h

FORMS += \
        mainwindow.ui \
    form.ui

完整工程代码:https://download.csdn.net/download/mccoy39082/10511888




你可能感兴趣的:(Qt)