QT多界面传值(信号)
main.cpp
#include "myapp.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
myapp w;
w.show();
return a.exec();
}
myapp.h
#ifndef MYAPP_H
#define MYAPP_H
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include "login.h"
class myapp : public QWidget
{
Q_OBJECT
public:
myapp(QWidget *parent = nullptr);
~myapp();
public:
QHBoxLayout *mybox;
QPushButton *mybtn;
login *l;
public slots:
void go_log();
signals:
void go(QString);
};
#endif // MYAPP_H
myapp.cpp
#include "myapp.h"
myapp::myapp(QWidget *parent)
: QWidget(parent)
{
this->mybox = new QHBoxLayout;
this->mybtn = new QPushButton;
this->mybox->addWidget(mybtn);
this->setLayout(mybox);
connect(this->mybtn,&QPushButton::clicked,this,&myapp::go_log);
}
myapp::~myapp()
{
}
void myapp::go_log()
{
this->l = new login;
l->show();
connect(this,&myapp::go,this->l,&login::go);
emit go("123");
this->close();
}
login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
class login : public QWidget
{
Q_OBJECT
public:
explicit login(QWidget *parent = nullptr);
public:
QHBoxLayout *lbox_x;
QLabel *lab;
public slots:
void go(QString);
};
#endif // LOGIN_H
login.cpp
#include "login.h"
login::login(QWidget *parent) : QWidget(parent)
{
}
void login::go(QString str)
{
this->lbox_x = new QHBoxLayout;
this->lab = new QLabel;
this->lab->setText(str);
this->lbox_x->addWidget(lab);
this->setLayout(lbox_x);
}