Qt中有关界面之间信息传递的两种方法

方法一:

       在某个界面定义公有成员函数,用来专门接收其它界面传递过来的信息。要接收信息时,主动调用该界面的公有成员函数。

方法二:

       在某个界面中自定义信号,然后用connect关联其它界面的槽函数。要接收信息时,使用emit来主动发送信号。

示例一:使用公有成员方法

firstwindow.h

#ifndef FIRSTWINDOW_H
#define FIRSTWINDOW_H

#include <QMainWindow>
#include <sencondwindow.h>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class FirstWindow; }
QT_END_NAMESPACE

class FirstWindow : public QMainWindow
{
    Q_OBJECT

public:
    FirstWindow(QWidget *parent = nullptr);
    ~FirstWindow();

private:
    Ui::FirstWindow *ui;

    QLabel *mylabel;
    QPushButton *mybutton1;
    QPushButton *mybutton2;
    QLineEdit *myle1;
    QLineEdit *myle2;
private slots:
    void buttonfun1();
    //void buttonfun2();
};
#endif // FIRSTWINDOW_H

firstwindow.cpp

#include "firstwindow.h"
#include "ui_firstwindow.h"


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

    QFont myfont("微软雅黑",20);

    mylabel = new QLabel("欢迎来到学生教务管理系统登录界面",this);
    mylabel->setGeometry(180,50,550,40);
    mylabel->setFont(myfont);

    myle1 = new QLineEdit(this);
    myle1->setGeometry(240,100,400,40);
    myle1->setFont(myfont);
    myle1->setPlaceholderText("请输入账号");
    myle1->setMaxLength(10);

    myle2 = new QLineEdit(this);
    myle2->setGeometry(240,150,400,40);
    myle2->setFont(myfont);
    myle2->setPlaceholderText("请输入密码");
    myle2->setMaxLength(10);
    myle2->setEchoMode(QLineEdit::Password);

    mybutton1 = new QPushButton("登录",this);
    mybutton1->setGeometry(260,200,160,30);

    mybutton2 = new QPushButton("注册",this);
    mybutton2->setGeometry(440,200,160,30);

    connect(mybutton1,&QPushButton::clicked,this,&FirstWindow::buttonfun1);

}

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

void FirstWindow::buttonfun1(){
    QString str1,str2;
    str1 = myle1->text();
    str2 = myle2->text();
    if(str1 == "海阔天空" && str2 == "123456"){
        sencondWindow* secw = new sencondWindow(this);
        secw->show();
        this->hide();
        secw->getattr(str1,str2);
    }
}

secondwindow.h

#ifndef SENCONDWINDOW_H
#define SENCONDWINDOW_H

#include <QMainWindow>
#include <firstwindow.h>
#include <QPushButton>
#include <QLabel>
#include <QString>
#include <QDebug>
namespace Ui {
class sencondWindow;
}

class sencondWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit sencondWindow(QWidget *parent = nullptr);
    ~sencondWindow();
    void getattr(QString mes1, QString mes2);//定义专门的公有函数去接收信息

private:
    Ui::sencondWindow *ui;
    QPushButton* get_back;
    QLabel* mylabel;
    QString message1;
    QString message2;
private slots:
    void mybuttonfun();
};

#endif // SENCONDWINDOW_H

secondwindow.cpp

#include "sencondwindow.h"
#include "ui_sencondwindow.h"

sencondWindow::sencondWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::sencondWindow)
{
    ui->setupUi(this);
    QFont myfont("宋体",10);

    mylabel = new QLabel("学生管理界面",this);
    mylabel->setGeometry(180,50,400,30);
    mylabel->setFont(myfont);

    get_back = new QPushButton("返回",this);
    get_back->setGeometry(900,20,50,30);
    get_back->setFont(myfont);

    connect(get_back,&QPushButton::clicked,this,&sencondWindow::mybuttonfun);
}

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

void sencondWindow::mybuttonfun(){
    QWidget *p = parentWidget();
    p->show();
    this->close();
}

void sencondWindow::getattr(QString mes1, QString mes2){
    this->message1 = mes1;
    this->message2 = mes2;
    qDebug()<<"账户名:"<<this->message1<<" "<<"密码:"<<mes2;
}

main.cpp

#include "firstwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FirstWindow w;
    w.show();
    return a.exec();
}

示例二:使用自定义信号

firstwindow.h

#ifndef FIRSTWINDOW_H
#define FIRSTWINDOW_H

#include <QMainWindow>
#include <sencondwindow.h>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class FirstWindow; }
QT_END_NAMESPACE

class FirstWindow : public QMainWindow
{
    Q_OBJECT

public:
    FirstWindow(QWidget *parent = nullptr);
    ~FirstWindow();

private:
    Ui::FirstWindow *ui;

    QLabel *mylabel;
    QPushButton *mybutton1;
    QPushButton *mybutton2;
    QLineEdit *myle1;
    QLineEdit *myle2;
private slots:
    void buttonfun1();
    //void buttonfun2();
//自定义信号
signals:
    void mysignal(QString,QString);
};
#endif // FIRSTWINDOW_H

firstwindow.cpp

#include "firstwindow.h"
#include "ui_firstwindow.h"


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

    QFont myfont("微软雅黑",20);

    mylabel = new QLabel("欢迎来到学生教务管理系统登录界面",this);
    mylabel->setGeometry(180,50,550,40);
    mylabel->setFont(myfont);

    myle1 = new QLineEdit(this);
    myle1->setGeometry(240,100,400,40);
    myle1->setFont(myfont);
    myle1->setPlaceholderText("请输入账号");
    myle1->setMaxLength(10);

    myle2 = new QLineEdit(this);
    myle2->setGeometry(240,150,400,40);
    myle2->setFont(myfont);
    myle2->setPlaceholderText("请输入密码");
    myle2->setMaxLength(10);
    myle2->setEchoMode(QLineEdit::Password);

    mybutton1 = new QPushButton("登录",this);
    mybutton1->setGeometry(260,200,160,30);

    mybutton2 = new QPushButton("注册",this);
    mybutton2->setGeometry(440,200,160,30);

    connect(mybutton1,&QPushButton::clicked,this,&FirstWindow::buttonfun1);

}

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

void FirstWindow::buttonfun1(){
    QString str1,str2;
    str1 = myle1->text();
    str2 = myle2->text();
    if(str1 == "海阔天空" && str2 == "123456"){
        sencondWindow* secw = new sencondWindow(this);
        //使用connect关联信号!!!
        connect(this,SIGNAL(mysignal(QString,QString)),secw,SLOT(fun(QString,QString)));
        //主动使用emit发送信号,记得传参!!!
        emit mysignal(str1,str2);

        secw->show();
        this->hide();
        //secw->getattr(str1,str2);
    }
}

secondwindow.h

#ifndef SENCONDWINDOW_H
#define SENCONDWINDOW_H

#include <QMainWindow>
#include <firstwindow.h>
#include <QPushButton>
#include <QLabel>
#include <QString>
#include <QDebug>
namespace Ui {
class sencondWindow;
}

class sencondWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit sencondWindow(QWidget *parent = nullptr);
    ~sencondWindow();
    //void getattr(QString mes1, QString mes2);//定义专门的公有函数去接收信息

private:
    Ui::sencondWindow *ui;
    QPushButton* get_back;
    QLabel* mylabel;
    QString message1;
    QString message2;
private slots:
    void mybuttonfun();
    void fun(QString str1,QString str2);
};

#endif // SENCONDWINDOW_H

secondwindow.cpp

#include "sencondwindow.h"
#include "ui_sencondwindow.h"

sencondWindow::sencondWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::sencondWindow)
{
    ui->setupUi(this);
    QFont myfont("宋体",10);

    mylabel = new QLabel("学生管理界面",this);
    mylabel->setGeometry(180,50,400,30);
    mylabel->setFont(myfont);

    get_back = new QPushButton("返回",this);
    get_back->setGeometry(900,20,50,30);
    get_back->setFont(myfont);

    connect(get_back,&QPushButton::clicked,this,&sencondWindow::mybuttonfun);
}

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

void sencondWindow::mybuttonfun(){
    QWidget *p = parentWidget();
    p->show();
    this->close();
}

//void sencondWindow::getattr(QString mes1, QString mes2){
//    this->message1 = mes1;
//    this->message2 = mes2;
//    qDebug()<<"账户名:"<<this->message1<<" "<<"密码:"<<mes2;
//}

void sencondWindow::fun(QString str1,QString str2){
    this->message1 = str1;
    this->message2 = str2;
    qDebug()<<"账户是:"<<this->message1<<" "<<"密码是:"<<this->message2;
}

main.cpp

#include "firstwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FirstWindow w;
    w.show();
    return a.exec();
}

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值