How QT implements communication between different windows

Method: Realize with qt signals and slots

 

1, First, the window that sends the signal, the custom signal, and the slot function.

 

Such as: From1

 

signals:

    void  SendData (QString s); //signal

 

private slots:

void  SendSlot (); //The function of transmitting the signal, the main function of this function is to send the SendData signal

 

 

//Slot function implementation

void Form1::SendSlot()

{

    QString s = ui->lineEdit->text();

    emit SendData(s);

}

 

 

 

 

//The signal and slot of the transfer button in Form1

 

 connect(ui->pushButton,&QPushButton::clicked,this,&Form1::SendSlot);

 

 

 

 

 

 

 

 

 

MainWindow main window

As the receiver of the signal, there is no need to customize the signal, just define the slot function for receiving the signal.

 

step:

1, First include the header file of the window (Form1) to send the signal

 

#include"form1.h"

 

define its object

Shape1 * f1;

 

Define a string to receive, the string sent by form1

QString Str;

 

//Define the receiving slot function

void Recive(QString s);

 

 

 

//Slot function implementation

void MainWindow::Recive(QString s)

{

    Str = s;

    ui->lineEdit->setText(Str);

}

 

 

 

 

 

//Implemented in the constructor.

f1 = new Form1;

 

//This old-fashioned way of writing is very suitable for sending signals.

connect(f1,SIGNAL(SendData(QString)),this,SLOT(Recive(QString)));

 

 

 

 

 

 

 

 

 

 

Effect:

678030838650430eb60d943a7897a8ff.png

 

 

 

 

 

 

 

 

 

 

Full code:



#include <QWidget>

namespace Ui {

class Form1;

}



class Form1 : public QWidget

{

    Q_OBJECT



public:

    explicit Form1(QWidget *parent = nullptr);

    ~Form1();



signals:

    void SendData(QString s);



private slots:

    void SendSlot();





private:

    Ui::Form1 *ui;

};













#include <QMainWindow>

#include"form1.h"



QT_BEGIN_NAMESPACE

namespace Ui { class MainWindow; }

QT_END_NAMESPACE



class MainWindow : public QMainWindow

{

    Q_OBJECT



public:

    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();



    Form1 * f1;



    QString Str;



private slots:

    void on_pushButton_clicked();





    void Recive(QString s);



private:

    Ui::MainWindow *ui;

};

















#include "form1.h"

#include "ui_form1.h"



Form1::Form1(QWidget *parent) :

    QWidget(parent),

    ui(new Ui::Form1)

{

    ui->setupUi(this);



    connect(ui->pushButton,&QPushButton::clicked,this,&Form1::SendSlot);

}



Form1::~Form1()

{

    delete ui;

}



void Form1::SendSlot()

{

    QString s = ui->lineEdit->text();

    emit SendData(s);

}









#include "mainwindow.h"

#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)

    : QMainWindow(parent)

    , ui(new Ui::MainWindow)

{

    ui->setupUi(this);

    f1 = new Form1;



    connect(f1,SIGNAL(SendData(QString)),this,SLOT(Recive(QString)));



}



MainWindow::~MainWindow()

{

    delete ui;

}





void MainWindow::on_pushButton_clicked()

{

    f1->show();

}



void MainWindow::Recive(QString s)

{

    Str = s;

    ui->lineEdit->setText(Str);

}

 

 

Guess you like

Origin blog.csdn.net/qq_58136559/article/details/129899366