widget.h
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void my_jump();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
second.h
cpp
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
public slots:
void jump_slot();
private:
Ui::Second *ui;
};
#endif // SECOND_H
main.cpp
cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Second s;
QObject::connect(&w,&Widget::my_jump,&s,&Second::jump_slot);
return a.exec();
}
widget.cpp
cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QString username = ui->lineEdit->text();
QString password = ui->lineEdit_2->text();
if (username == "admin" && password == "123456")
{
QMessageBox msg(QMessageBox::Information,"登录","登录成功",QMessageBox::Ok,this);
int ret = msg.exec();
if(ret == QMessageBox::Ok)
{
this->close();
emit my_jump();
}
}
else
{
QMessageBox msg(QMessageBox::Critical,"错误","账号和密码不匹配,是否重新登陆",QMessageBox::Yes|QMessageBox::No);
int ret = msg.exec();
if(ret == QMessageBox::Yes)
{
ui->lineEdit_2->clear();
}
else
{
this->close();
}
}
}
void Widget::on_pushButton_2_clicked()
{
QMessageBox msg(QMessageBox::Question,"问题","您是否要退出登录?",QMessageBox::Yes|QMessageBox::No);
int ret = msg.exec();
if(ret == QMessageBox::Yes)
{
this->close();
}
else
{
return;
}
}
second.cpp
cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
}
Second::~Second()
{
delete ui;
}
void Second::jump_slot()
{
this->show();
}