Call the main window UI control in the child window

Sometimes we need to change the UI state in the main window through some events (send some signals, or button click events) in the sub-window, such as changing the content of the label, LineEdit, etc. in the main window, or sending a signal to the main window when the serial port receives The window replies, the main window changes the signal light (UI) color to achieve "signal visualization", so how to call the controls in the main window?


code part

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "userwidget.h"//引用 用户界面头文件
#include "ui_userwidget.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void changeText(int num);//调用UI函数
    userwidget * userw = NULL;//用来保存用户界面实例化对象地址

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    userw = new userwidget();//实例化用户界面对象 
    //注意:不能将this父窗口的指针直接传给子窗口 例如:userw = new userwidget(this);
    
    userw->getWidget(this); //写一个接口函数将父窗口传给子窗口
    
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        //this->hide();
        userw->show();
    });

    //接收用户界面的返回信号
    /*connect(this->userw,&userwidget::back,[=](){
        //this->userw->hide();
        //this->show();
    });*/
}

Widget::~Widget()
{
    delete ui;
}
//自定义函数 调用UI
void Widget::changeText(int num)
{
    switch(num)
    {
        case 1:
            ui->lineEdit->setText("我是第一名!");
            break;
        case 2:
            ui->lineEdit->setText("我是第二名!");
            break;
        case 3:
            ui->lineEdit->setText("我是第三名!");
            break;
        default:
            break;
    }
}

userwidget.h

#ifndef USERWIDGET_H
#define USERWIDGET_H

#include <QWidget>
class Widget;//主窗口声明

namespace Ui {
class userwidget;
}

class userwidget : public QWidget
{
    Q_OBJECT

public:
    explicit userwidget(QWidget *parent = nullptr);
    ~userwidget();

    //主函数调用 将主窗口给到子窗口
    void getWidget(Widget * pWidget)
    {
        wi = pWidget;
    }
//signals:
//    void back();//定义一个信号
private slots:
    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

private:
    Ui::userwidget *ui;

    Widget *wi;
};

#endif // USERWIDGET_H

userwidget.cpp

#include "userwidget.h"
#include "ui_userwidget.h"
#include <QPushButton>
#include <QDebug>
#include "widget.h"

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

    /*
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        //发信号给主界面
        emit this->back();
    });
    */

    //将传过来的父窗口进行强制类型转换
    //wi = static_cast<Widget*>(parent);
}

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

void userwidget::on_pushButton_2_clicked()
{
    wi->changeText(1);
}

void userwidget::on_pushButton_3_clicked()
{
    wi->changeText(2);
}

void userwidget::on_pushButton_4_clicked()
{
     wi->changeText(3);
}

This is the first time to write, if you have any questions, you can ask them in the comments, thank you!

Guess you like

Origin blog.csdn.net/qq_42516331/article/details/126223539