完善登录界面
1.点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容 "账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
2.如果账号和密码匹配,则弹出信息对话框,给出提示信息为"登录成功",给出一个按钮ok,点击ok后,关闭整个登录界 面,跳转到其他界面
3.点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yesIno,点击yes,则直接关闭整个登 录界面,如果点击no则进行进行登录
要求:对象版和静态成员函数版至少各实现一个
//主界面头文件
#ifndef QQ_H
#define QQ_H
#include <QWidget>
#include <QPushButton>
#include <QIcon>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
#include <QMessageBox>
namespace Ui {
class QQ;
}
class QQ : public QWidget
{
Q_OBJECT
signals:
void jump(); //自定义跳转信号
public:
explicit QQ(QWidget *parent = nullptr);
~QQ();
private slots:
void on_btn1_clicked();
void on_btn2_clicked();
private:
Ui::QQ *ui;
QPushButton *btn1; //登录
QPushButton *btn2; //取消
QLineEdit *usr; //用户账号
QLineEdit *passwd; //密码
QLabel *label1;
QLabel *label2;
QLabel *label3;
QCheckBox *checkbox1;
QCheckBox *checkbox2;
};
#endif // QQ_H
//跳转界面头文件
#ifndef LOGIN_H
#define LOGIN_H
#include <QWidget>
#include <QLabel>
#include <QFont>
namespace Ui {
class login;
}
class login : public QWidget
{
Q_OBJECT
public slots:
void jump_slot();
public:
explicit login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
QLabel *label1;
QLabel *label2;
};
#endif // LOGIN_H
//主界面功能函数
#include "qq.h"
#include "ui_qq.h"
//btn1的槽函数
void QQ::on_btn1_clicked()
{
QString u=usr->text();
QString p=passwd->text();
if(u == "admin" && p=="123456")
{
QMessageBox Ibox(QMessageBox::Information, //图标
"提示", //对话框标题
"登录成功", //文本框内容
QMessageBox::Ok, //提供的按钮
this); //父组件
//将对象进入运行态
int ret=Ibox.exec();
//对用户选择的结果进行判断
if(ret == QMessageBox::Ok)
{
emit jump();
this->close();
}
}
else
{
//实例化一个消息对话框的对象
QMessageBox qbox(QMessageBox::Critical, //图标
"Error", //对话框标题
"账户密码不匹配,是否重新登录", //文本框内容
QMessageBox::Ok|QMessageBox::Cancel, //提供的按钮
this); //父组件
//将对象进入运行态
int ret=qbox.exec();
//对用户选择的结果进行判断
if(ret == QMessageBox::Ok)
{
passwd->clear();
}
else if(ret == QMessageBox::Cancel)
{
this->close();
}
}
}
void QQ::on_btn2_clicked()
{
//静态成员函数版
QMessageBox::StandardButton ret=QMessageBox::question(
this,
"提示", //对话框标题
"是否确定要退出登录?", //文本框内容
QMessageBox::Yes|QMessageBox::No, //提供的按钮
QMessageBox::Yes);
//对用户选择的结果进行判断
if(ret == QMessageBox::Yes)
{
this->close(); //关闭登录界面
}
else if(ret == QMessageBox::No)
{
qDebug()<<"choose NO";
}
}
QQ::QQ(QWidget *parent) :
QWidget(parent),
ui(new Ui::QQ)
{
ui->setupUi(this);
this->setFixedSize(480,350);
this->setWindowTitle("QQ");
this->setWindowIcon(QIcon("D:/Qt/day1/lg.png"));
btn1 = new QPushButton("登录", this);
btn1->resize(80,30);
btn1->move(140,280);
btn1->setIcon(QIcon("D:/Qt/day1/pic.png"));
connect(btn1,SIGNAL(clicked()),this,SLOT(on_btn1_clicked()));
btn2 = new QPushButton("取消", this);
btn2->resize(80,30);
btn2->move(240,280);
btn2->setIcon(QIcon("D:/Qt/day1/pic.png"));
connect(btn2,SIGNAL(clicked()),this,SLOT(on_btn2_clicked()));
label1 = new QLabel(this);
label1->resize(480,160);
label1->setScaledContents(true);
label1->setPixmap(QPixmap("D:/Qt/day1/backgrd.png"));
label2 = new QLabel(this);
label2->resize(25,25);
label2->move(120,172);
label2->setScaledContents(true);
label2->setPixmap(QPixmap("D:/Qt/day1/logo.png"));
label3 = new QLabel(this);
label3->resize(25,25);
label3->move(120,207);
label3->setScaledContents(true);
label3->setPixmap(QPixmap("D:/Qt/day1/logo.png"));
usr = new QLineEdit(this);
usr->resize(200, 30);
usr->move(150, 170);
usr->setPlaceholderText("账号");
passwd = new QLineEdit(this);
passwd->resize(200, 30);
passwd->move(150, 205);
passwd->setEchoMode(QLineEdit::Password);
passwd->setPlaceholderText("密码");
checkbox1 = new QCheckBox("自动登录", this);
checkbox1->move(150,245);
checkbox2 = new QCheckBox("记住密码", this);
checkbox2->move(250,245);
}
QQ::~QQ()
{
delete ui;
}
//跳转界面功能函数
#include "login.h"
#include "ui_login.h"
#include <QIcon>
void login::jump_slot()
{
this->show();
}
login::login(QWidget *parent) :
QWidget(parent),
ui(new Ui::login)
{
ui->setupUi(this);
this->setFixedSize(350,800);
this->setWindowTitle("QQ");
this->setWindowIcon(QIcon("D:/Qt/day1/lg.png"));
label1 = new QLabel(this);
label1->resize(350,800);
label1->setScaledContents(true);
label1->setPixmap(QPixmap("D:/Qt/day1/backgrd.png"));
label2 = new QLabel(this);
label2->resize(200,100);
label2->setText("Welcome to QQ");
QFont font("SimSun",14); //设置字体为宋体
label2->setFont(font);
label2->move(120,172);
label2->setScaledContents(true);
}
login::~login()
{
delete ui;
}
//主函数
#include "qq.h"
#include "login.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQ w;
w.show();
login s;
QObject::connect(&w,&QQ::jump,&s,&login::jump_slot);
return a.exec();
}
结果:
账户密码正确时显示登录成功,点击ok后跳转
跳转界面:
账户密码不匹配时显示:
点击ok则清空密码框重新输入,点击cancel则关闭界面