?main.c
#include "widget.h"
#include <QApplication>
#include "success.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
success s;
QObject::connect(&w,&Widget::login_signal,&s,&success::login_slot);
return a.exec();
}
?登录界面
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QDebug>
#include<QMessageBox>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void Cancelbtn();
void Loginbtn();
signals:
void login_signal();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
?widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->userlab->setPixmap(QPixmap(":/pictrue/userName.jpg"));
ui->userlab->setScaledContents(true);
ui->passwdlab->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
ui->passwdlab->setScaledContents(true);
ui->passwdedit->setEchoMode(QLineEdit::Password);
//将取消按钮使用qt4版本的连接到自定义的槽函数中
connect(ui->cancelbtn,SIGNAL(clicked()),this,SLOT(Cancelbtn()));
//将登录按钮使用qt5版本的连接到自定义的槽函数中
connect(ui->loginbtn,&QPushButton::clicked,this,&Widget::Loginbtn);
}
Widget::~Widget()
{
delete ui;
}
void Widget::Cancelbtn()
{
//弹出一个问题对话框
int ret=QMessageBox::question(this,"问题","您是否确定要退出登录?",QMessageBox::Yes|QMessageBox::No);
if(ret==QMessageBox::Yes)
{
//Yes后,关闭登录界面
close();
}else
{
//No后,关闭对话框
this->show();
}
}
void Widget::Loginbtn()
{
//在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456"
if(ui->useredit->text()=="admin"&ui->passwdedit->text()=="123456")
{
//登录成功
qDebug()<<"登陆成功"<<endl;
close();
//基于静态成员函数版本的对话框
int ret=QMessageBox::information(this,"提示","登陆成功",QMessageBox::Ok);
//点击ok转到下一个界面
if(ret==QMessageBox::Ok)
{
emit login_signal();
}
}else
{
//登录失败
qDebug()<<"登录失败"<<endl;
//基于属性版本的错误提示框
//1.用该类实例化一个对象
QMessageBox miss_msg(QMessageBox::Critical,
"错误",
"账号和密码不匹配,是否重新登录",
QMessageBox::Yes|QMessageBox::No,
this);
//2.需要调用exec()弹出对话框
int ret=miss_msg.exec();
if(ret==QMessageBox::Yes)
{
//点击yes清除密码框中的内容
ui->passwdedit->clear();
}else
{
//点击No按钮,则直接关闭登录界面
close();
}
}
}
?登录成功跳转页面
success.h
#ifndef SUCCESS_H
#define SUCCESS_H
#include <QWidget>
namespace Ui {
class success;
}
class success : public QWidget
{
Q_OBJECT
public:
explicit success(QWidget *parent = nullptr);
~success();
public slots:
void login_slot();
private:
Ui::success *ui;
};
#endif // SUCCESS_H
success.cpp?
#include "success.h"
#include "ui_success.h"
success::success(QWidget *parent) :
QWidget(parent),
ui(new Ui::success)
{
ui->setupUi(this);
}
success::~success()
{
delete ui;
}
void success::login_slot()
{
this->show();
}