How does Qt control the control of MainWindow in other cpp files

How does Qt control the control of MainWindow in other cpp files_qt control controls another cpp file window_programmer's blog who only knows git clone-CSDN blog

About Qt Two .cpp files call each other's functions_Qt calls functions in another cpp_Xi Shuang Ruoya's Blog-CSDN Blog

QT common type conversion_qt uint_Fei classmates' blog-CSDN blog

Conversion between QByteArray, QString, char, decimal, and hexadecimal is common in QT_qbytearray to decimal_Nina_小哥的博客-CSDN博客

"QT(C++) Development Engineer"_Qt5 Interface Development Engineer Course, Embedded Linux Development Engineer Course, C++ Software Engineer Course-CSDN Blog

qstring merge string in qt
https://www.yii666.com/blog/361427.html

QT formatting on QString (zero padding/hex conversion)_qstring formatting_Zhishou Blog-CSDN Blog

Advanced Qt development: QString formatted output double value_qstring double_High precision computer vision blog-CSDN blog

QString class—number_qstring::number_Acuity.'s Blog - CSDN Blog

LineEdit->setText() in qt outputs double - Developer Blog
https://tool.4xseo.com/a/56309.html

Qt: Convert QString to double type_qstring to double problem_xxgxgx's Blog-CSDN Blog

Qt/C++ Compile Map Comprehensive Application 59-Longitude and Latitude Coordinate Correction_feiyangqingyun's Blog-CSDN Blog

Method 1
Add a public pointer type static member variable of this class to the MainWindow class, and other files use this pointer to manipulate the controls in MainWindow.

Explanation:
The following shows how to create a listForm interface by pressing a button from mainWindow, and then control the edit box of the mainWindow window through an Insert and a Delete button on the listForm interface.

The mainWindow.h file is as follows:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <listform.h>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

public:
    static MainWindow *mutualUi;//!!!!!!!!!指针类型静态成员变量
    void setLinetext(QString s);
private slots:
    void on_pushButton_clicked();
};

#endif // MAINWINDOW_H


static MainWindow *mutualUi;//!!! Static member variables of pointer type
Why do you need to refer to this statically, detailed explanation of static in c++

The mainWindow.cpp file is as follows:

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

MainWindow *MainWindow::mutualUi = nullptr;//!!!!初始化,非常重要

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mutualUi = this;//!!!赋值,非常重要
}

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

void MainWindow::setLinetext(QString s)
{
    ui->lineEdit->setText(s);
}

void MainWindow::on_pushButton_clicked()
{
    listForm *l;
    l = new listForm();
    l->show();

}


MainWindow *MainWindow::mutualUi = nullptr;//! ! ! ! Initialization is very important
mutualUi = this;//! ! ! assignment, very important

listForm.cpp file

#include "listform.h"
#include "ui_listform.h"
#include "mainwindow.h"

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

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

void listForm::on_pushButton_clicked()
{
    MainWindow::mutualUi->setLinetext("1111");//通过mainwindow的指针设置文本
}

void listForm::on_pushButton_2_clicked()
{
    MainWindow::mutualUi->setLinetext("");//通过mainwindow的指针设置文本
}


MainWindow::mutualUi->setLinetext("1111");//Set the text
listForm.h file through the pointer of mainwindow:

#ifndef LISTFORM_H
#define LISTFORM_H

#include <QWidget>

namespace Ui {
class listForm;
}

class listForm : public QWidget
{
    Q_OBJECT

public:
    explicit listForm(QWidget *parent = 0);
    ~listForm();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::listForm *ui;
};

#endif // LISTFORM_H


Effect display:
run:

Create a listForm window:

Click Insert:

Click to delete:

Guess you like

Origin blog.csdn.net/m0_37777700/article/details/132457826