方法一:
在某个界面定义公有成员函数,用来专门接收其它界面传递过来的信息。要接收信息时,主动调用该界面的公有成员函数。
方法二:
在某个界面中自定义信号,然后用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();
}