一、完善登录框
点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。
如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面
点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录
要求:消息对话框,对象版和静态成员函数版至少各实现一个
第二个界面,second.h:
- #ifndef SECOND_H
- #define SECOND_H
-
- #include
-
- namespace Ui {
- class Second;
- }
-
- class Second : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Second(QWidget *parent = nullptr);
- ~Second();
-
- public slots:
- void jump_slot(); //跳转槽,用来接收从widget发送的信号
-
- private:
- Ui::Second *ui;
- };
-
- #endif // SECOND_H
第二个界面,second.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();
- }
widget.h:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include "second.h"
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- private:
- QLabel *lab1; //背景
- QLabel *lab2; //用户图标
- QLabel *lab3; //密码图标
- QLineEdit *edit1; //用户名/手机/邮箱
- QLineEdit *edit2; //密码
- QPushButton *btn1; //登录
- QPushButton *btn2; //退出
- QMessageBox errbox; //错误对话框定义
-
- Second* s1; //第二个界面定义
- signals:
- void jump(); //发送信号与第二个界面second槽反应
-
- public slots:
- void login_slot(); //登录按钮点击事件接收槽
- void cancel_slot(); //取消按钮点击事件接受槽
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
- };
- #endif // WIDGET_H
widget.cpp:
- #include "widget.h"
-
-
- Widget::Widget(QWidget *parent)
- : QWidget(parent)
- {
- this->setFixedSize(500,500);
- this->setWindowTitle("hqyj login...");
- this->setWindowIcon(QIcon("D:\\qianrushi\\icon\\wodepeizhenshi.png"));
-
- //实例化一个标签,给定初始文本内容并指定父组件
- lab1=new QLabel(this);
-
- lab1->resize(500,200);
- lab1->move(0,0);
- lab1->setPixmap(QPixmap("D:\\qianrushi\\icon\\logo.png"));
- lab1->setScaledContents(true);
- //lab1->setGeometry(0,0,500,200);
-
-
- lab2=new QLabel(this);
- lab2->resize(30,30);
- lab2->move(80,250);
- lab2->setPixmap(QPixmap("D:\\qianrushi\\icon\\userName.jpg"));
- lab2->setScaledContents(true);
-
-
-
- edit1=new QLineEdit(this);
- edit1->setGeometry(lab2->x()+50,lab2->y()-10,300,50);
- edit1->setPlaceholderText("hqyj账号/手机/邮箱");
-
-
- lab3=new QLabel(this);
- lab3->setPixmap(QPixmap("D:\\qianrushi\\icon\\passwd.jpg"));
- lab3->setGeometry(lab2->x(),lab2->y()+50,30,30);
- lab3->setScaledContents(true);
-
- edit2=new QLineEdit(this);
- edit2->setGeometry(lab3->x()+50,lab3->y()-10,300,50);
- edit2->setPlaceholderText("密码");
- edit2->setEchoMode(QLineEdit::Password);
-
- btn1=new QPushButton(QIcon("D:\\qianrushi\\icon\\login.png"),"登录",this);
- btn1->resize(100,40);
- btn1->move(lab3->x()+100,lab3->y()+80);
-
- btn2=new QPushButton(QIcon("D:\\qianrushi\\icon\\cancel.png"),"退出",this);
- btn2->resize(100,40);
- btn2->move(btn1->x()+150,btn1->y());
-
- //给第二个界面申请空间
- s1 = new Second;
- //连接弹窗的信号与槽
- connect(this,&Widget::jump,s1,&Second::jump_slot);
-
- //连接登录的信号与槽
- connect(btn1,&QPushButton::clicked,this,&Widget::login_slot);
- //连接取消的信号与槽
- connect(btn2,&QPushButton::clicked,this,&Widget::cancel_slot);
- }
-
- Widget::~Widget()
- {
- }
-
- void Widget::login_slot()
- {
- if(edit1->text()!="admin" || edit2->text()!="123456")
- {
- //登录失败,弹出错误对话框
- errbox.setText("账号密码不匹配,是否重新登录");
- errbox.setWindowTitle("错误对话框");
- errbox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
- errbox.setDefaultButton(QMessageBox::Yes);
- int ret1=errbox.exec();
- if(ret1==QMessageBox::Yes)
- {
- //同意,隐藏错误对话框,清空账号密码
- errbox.hide();
- edit1->clear();
- edit2->clear();
- }else if(ret1==QMessageBox::Cancel)
- {
- //取消,关闭错误对话框
- errbox.close();
- }
- }else{
- int ret2=QMessageBox::information(this,"信息对话框","登录成功",QMessageBox::Ok,QMessageBox::Ok);
- if(ret2 == QMessageBox::Ok)
- {
- //同意登录,跳转
- this->close();
- emit jump();
- }
- }
- }
-
- void Widget::cancel_slot()
- {
- QMessageBox quesbox(QMessageBox::Question,
- "问题对话框",
- "确定退出?",
- QMessageBox::Yes|QMessageBox::No,
- this);
- //取消,弹出问题对话框
- int ret3=quesbox.exec();
- if(ret3 == QMessageBox::Yes)
- {
- //同意退出,退出程序
- quesbox.close();
- this->close();
- }else if(ret3 == QMessageBox::No)
- {
- //取消,继续登录
- quesbox.hide();
- }
- }
-
main.cpp:
- #include "widget.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- return a.exec();
- }
二、思维导图:有道云笔记