作业:
完善登录界面
点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
要求:对象版和静态成员函数版至少各实现一个
头文件
//mywindow.h
#ifndef MYWINDOW_H
#define MYWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QCheckBox>
#include <QDebug>
#include <QMessageBox>
class MyWindow : public QMainWindow
{
Q_OBJECT
public:
MyWindow(QWidget *parent = 0);
~MyWindow();
QLabel *lable1;//界面lable
QLabel *lable2;//账户lable
QLabel *lable3;//密码
QLabel *lable6 ; //二维码
QLineEdit *edit1;
QLineEdit *edit2;
QPushButton *key1;//登录
QPushButton *key4;//取消
QPushButton *key2;//找回密码
QPushButton *key3;//注册账号
QCheckBox *box1;//自动登录复选框
QCheckBox *box2;//记住密码复选框
signals:
void login_sig();//登录信号
void cancel_sig();//取消信号
void login_ss();//登录成功信号
void login_ee();//登陆失败信号
void jump();
public slots:
void login_slot();//登录槽函数
void login_s();//登陆成功对话框
void login_e();//登陆失败对话框
void cancel_slot();//取消槽函数
void cancel_d();
};
#endif // MYWINDOW_H
//second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public slots:
void jump_slot();//自定义跳转函数声明
public:
explicit Second(QWidget *parent = nullptr);
~Second();
private:
Ui::Second *ui;
};
#endif // SECOND_H
功能函数
#include "mywindow.h"
MyWindow::MyWindow(QWidget *parent)
: QMainWindow(parent)
{
//更改大小
this->setFixedSize(427,301);
//更改项目名称
this->setWindowTitle("QQ");
//更改图标
this->setWindowIcon(QIcon("D:/QT/icon/icon_nhgbq8i4bf/aichegujiabeifen6.png"));
//界面lable
lable1 = new QLabel(this);
lable1->resize(427,100);
lable1->move(0,0);
lable1->setScaledContents(true);
lable1->setPixmap(QPixmap("D:\\QT\\icon\\icon_nhgbq8i4bf\\1.png"));
//账户lable
lable2 = new QLabel(this);
lable2->resize(25,25);
lable2->move(101,120);
lable2->setScaledContents(true);
lable2->setPixmap(QPixmap("D:\\QT\\icon\\icon_nhgbq8i4bf\\denglu.png"));
edit1 = new QLineEdit(this);
edit1->resize(220,30);
edit1->move(135,120);
edit1->setPlaceholderText("QQ号码/手机/邮箱");
//密码
lable3 = new QLabel(this);
lable3->resize(25,25);
lable3->move(101,170);
lable3->setScaledContents(true);
lable3->setPixmap(QPixmap("D:\\QT\\icon\\icon_nhgbq8i4bf\\QQ.png"));
edit2 = new QLineEdit(this);
edit2->resize(220,30);
edit2->move(135,170);
edit2->setPlaceholderText("密码");
edit2->setEchoMode(QLineEdit::Password);
//找回密码
key2 = new QPushButton(this);
key2->resize(70,20);
key2->move(301,220);
key2->setText("找回密码");
//注册账号
key3 = new QPushButton(this);
key3->resize(70,20);
key3->move(0,281);
key3->setText("注册账号");
//二维码
lable6 = new QLabel(this);
lable6->resize(25,25);
lable6->move(401,271);
lable6->setScaledContents(true);
lable6->setPixmap(QPixmap("D:\\QT\\icon\\icon_nhgbq8i4bf\\2.png"));
//登录
//重新设置大小
key1 = new QPushButton(this);
key1->resize(110,35);
//设置文本类容
key1->setText("登录");
//设置图标
key1->setIcon(QIcon("D:\\QT\\icon\\icon_nhgbq8i4bf\\denglu_1.png"));
//将组件移动走
key1->move(101,250);
//设置背景颜色
key1->setStyleSheet("background-color:skyblue;"
"border-radius:10px;");
//取消
key4 = new QPushButton(this);
key4->resize(110,35);
//设置文本类容
key4->setText("取消");
//设置图标
key4->setIcon(QIcon("D:\\QT\\icon\\icon_nhgbq8i4bf\\quxiao.png"));
//将组件移动走
key4->move(251,250);
//设置背景颜色
key4->setStyleSheet("background-color:skyblue;"
"border-radius:10px;");
//checkbox1
box1 = new QCheckBox(this);
box1->setText("自动登录");
box1->resize(70,20);
box1->move(105,220);
//checkbox2
box2 = new QCheckBox(this);
box2->setText("记住密码");
box2->resize(70,20);
box2->move(205,220);
//signal and slot
//手动连接登录信号到自定义的登录槽函数中
//connect(this->key1,&QPushButton::clicked,this,&MyWindow::login_slot);
connect(this->key1,&QPushButton::clicked,this,&MyWindow::login_sig);
connect(this->key1,&QPushButton::clicked,this,&MyWindow::login_slot);
//手动连接取消信号到自定义的取消槽函数中
connect(this->key4,&QPushButton::clicked,this,&MyWindow::cancel_sig);
connect(this->key4,&QPushButton::clicked,this,&MyWindow::cancel_slot);
}
MyWindow::~MyWindow()
{
}
void MyWindow::login_slot()
{
if(this->edit1->text() == "admin" && this->edit2->text() == "123456")
emit login_ss();
else
emit login_ee();
}
void MyWindow::login_s()
{
int res = QMessageBox::information(this,
"登录",
"登录成功",
QMessageBox::Ok,
QMessageBox::Ok);
if(QMessageBox::Ok == res)
{
emit this->jump();
this->close();
}
}
void MyWindow::login_e()
{
QMessageBox box(QMessageBox::Critical,
"错误",
"账号或密码不匹配,是否重新登录",
QMessageBox::Ok | QMessageBox::Cancel,
this);
//将对话框进入运行态
int res = box.exec();
if(QMessageBox::Ok == res)
{
this->edit2->clear();
}
else if(QMessageBox::Cancel == res)
{
this->close();
}
}
void MyWindow::cancel_slot()
{
connect(this->key4,&QPushButton::clicked,this,&MyWindow::cancel_d);
}
void MyWindow::cancel_d()
{
int res = QMessageBox::question(this,
"登录",
"是否要确定退出登录",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
if(QMessageBox::Yes == res)
this->close();
}
//second.h
#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();
}
主函数
#include "mywindow.h"
#include <QApplication>
#include "second.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWindow w;
w.show();
Second s;
QObject::connect(&w,&MyWindow::jump,&s,&Second::jump_slot);
return a.exec();
}
效果展示