完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到新的界面中
如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户点击Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
要求:基于属性版和基于静态成员函数版至少各用一个
要求:尽量每行代码都有注释
- #include "widget.h"
- #include "form.h"
-
- #include
-
- int main(int argc, char* argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- Form f;
- QWidget::connect(&w, &Widget::jump_signal, &f, &Form::jump_slot);//绑定跳转信号和跳转槽函数
- return a.exec();
- }
- #ifndef FORM_H
- #define FORM_H
-
- #include
-
- namespace Ui
- {
- class Form;
- }
-
- class Form : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Form(QWidget* parent = nullptr);
- ~Form();
- public slots:
- void jump_slot(); //跳转槽函数声明
- private:
- Ui::Form* ui;
- };
-
- #endif // FORM_H
- #include "form.h"
- #include "ui_form.h"
-
- Form::Form(QWidget* parent) :
- QWidget(parent),
- ui(new Ui::Form)
- {
- ui->setupUi(this);
- }
- void Form::jump_slot()
- {
- this->show();
- }
- Form::~Form()
- {
- delete ui;
- }
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
-
-
- QT_BEGIN_NAMESPACE
- namespace Ui
- {
- class Widget;
- }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget* parent = nullptr);
- ~Widget();
- signals:
- void jump_signal(); //跳转信号
- public slots:
- void my_slot(); //退出程序槽函数
- void do_login(); //登录
-
-
-
- private:
- Ui::Widget* ui;
- };
- #endif // WIDGET_H
- #include "widget.h"
- #include "ui_widget.h"
-
- Widget::Widget(QWidget* parent)
- : QWidget(parent)
- , ui(new Ui::Widget)
-
- {
- ui->setupUi(this);
- //加载动画
- QMovie* mv = new QMovie(":/icon/qq2.gif");
- ui->label->setMovie(mv);
- mv->start();
-
- ui->edit_pswd->setEchoMode(QLineEdit::Password);//把行编辑器设置为密码模式
- connect(ui->btn_exit, SIGNAL(clicked()), this, SLOT(my_slot()));//退出按钮绑定退出槽函数
- connect(ui->btn_login, &QPushButton::clicked, this, &Widget::do_login);//登录按钮绑定登录槽函数
-
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- void Widget::my_slot()
- {
- //实例化消息对话框
- QMessageBox msg(QMessageBox::Icon::Question, "询问", "是否退出程序?", QMessageBox::Yes | QMessageBox::No, this);
- int res = msg.exec();
- if(QMessageBox::Yes == res) {
- this->close();
- }
- }
- void Widget::do_login()
- {
-
- if(ui->edit_user->text() == "admin" && ui->edit_pswd->text() == "123456") {
- QMessageBox::information(this, "提示", "登录成功!", QMessageBox::Ok );
- this->close();
- emit jump_signal();
-
- } else {
- int res = QMessageBox::critical(this, "错误", "账户或密码错误! 是否重新登录?", QMessageBox::Yes | QMessageBox::No);
- if(QMessageBox::Yes == res) {
- ui->edit_pswd->clear();
- ui->edit_user->clear();
- } else {
-
- this->close();
-
- }
-
-
- }
- }
-