思维导图:
第一个界面头文件:
#define WIDGET_H
#include <QWidget>
#include <QMessageBox>
#include <QDebug>
#include <QRandomGenerator>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
signals:
void my_jump();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
第一个界面源文件:
#include "widget.h"
#include "ui_widget.h"
#include "form.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
if(ui->lineEdit->text() == "admin" && ui->lineEdit_2->text() == "123456")
{
QMessageBox msg(QMessageBox::NoIcon,
"提示",
"登陆成功",
QMessageBox::Ok,
this
);
int ret = msg.exec();
if(ret == QMessageBox::Ok)
{
this->close();
emit my_jump();
}
}else
{
int ret = QMessageBox::warning(this,"警告","账号或者密码输入错误!是否重新输入",QMessageBox::Yes | QMessageBox::No);
if(ret == QMessageBox::Yes)
{
ui->lineEdit_2->setText("");
}else
{
close();
}
}
}
void Widget::on_pushButton_2_clicked()
{
close();
}
第二个界面的头文件:
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public:
void my_jump();
private:
Ui::Form *ui;
};
#endif // FORM_H
第二个界面的源文件:
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
void Form::my_jump()
{
this->show();
}
主文件:
#include "widget.h"
#include "form.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Form s;
QObject::connect(&w,&Widget::my_jump,&s,&Form::my_jump);
return a.exec();
}
结果: