赞
踩
练习:将登录框进行优化
点击取消按钮,弹出一个问题对话框,询问"是否确定要退出登录",给出两个按钮,Yes|No,用户点击Yes后,关闭界面,用户点击No后,继续回到登录界面
点击登录按钮,当账号和密码验证成功,弹出信息对话框,给出信息"登录成功",给出按钮ok,用户点击ok后,发射一个自定义信号后关闭登录界面
当账号和密码验证失败,则弹出错误对话框,给出信息"账号和密码不匹配,是否重新登录",并给出两个按钮Yes|No,用户点击Yes后,回到登录界面,并清空密码输入框中的内容。用户点击No后,直接关闭登录界面
要求:至少使用一个基于属性版本和一个基于静态成员函数版本的消息对话框
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include <QWidget>
- #include <QDebug>
- #include <QIcon>
- #include <QLabel>
- #include <QLineEdit>
- #include <QPushButton>
- #include <iostream>
- #include <QMessageBox>
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
- signals:
- //声明一个登录信号
- void loginSuccess();
- private slots:
-
- void on_loginButton_clicked();
- void on_cancelButton_clicked();
- private:
- // 定义界面中的组件
- QLineEdit *edt_user;
- QLineEdit *edt_pwd;
-
-
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- 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);
- //设置固定尺寸
- this->setFixedSize(555,375);
- //设置窗口标题
- this->setWindowTitle("QQ");
- //设置窗口图标
- this->setWindowIcon(QIcon("E:\\c++\\qq.jpg"));
- //设置背景色
- this->setBackgroundRole(QPalette::Light);
- //设置允许填充背景
- this->setAutoFillBackground(true);
-
- //定义一个标签,做上半背景
- QLabel *lab_bgp = new QLabel(this);
- //设置尺寸
- lab_bgp->resize(555,150);
- //设置透明度
- lab_bgp->setWindowOpacity(0.3);
- //设置按钮颜色
- lab_bgp->setStyleSheet("background-color:rgb(137,207,240)");
-
- //定义一个标签头像
- QLabel *lab_hp = new QLabel(this);
- //设置尺寸
- lab_hp->resize(80,80);
- //设置头像图标
- lab_hp->setPixmap(QPixmap("E:\\c++\\qq1.png"));
- lab_hp->move(225,78);
- //设置填充格式:true-自适应大小
- lab_hp->setScaledContents(true);
-
- //定义一个账号标签
- QLabel *lab_user = new QLabel("账户:",this);
- //设置尺寸
- lab_user->resize(19,23);
- //设置头像图标
- lab_user->setPixmap(QPixmap(":/new/prefix1/icon/userName.jpg"));
- lab_user->move(120,185);
- //设置填充格式:true-自适应大小
- lab_user->setScaledContents(true);
-
- //定义一个密码标签
- QLabel *lab_pwd = new QLabel("密码:",this);
- //设置尺寸
- lab_pwd->resize(19,22);
- //设置头像图标
- lab_pwd->setPixmap(QPixmap(":/new/prefix1/icon/passwd.jpg"));
- lab_pwd->move(120,240);
- //设置填充格式:true-自适应大小
- lab_pwd->setScaledContents(true);
-
- //定义账号的行编辑
- edt_user = new QLineEdit(this);
- //设置尺寸和位置
- edt_user->resize(280,45);
- edt_user->move(140,170);
- //设置默认值
- edt_user->setPlaceholderText("账户");
- //设置边框只留底行
- edt_user->setStyleSheet("border: none; border-bottom: 1px solid black;");
-
- //定义密码的行编辑
- edt_pwd = new QLineEdit(this);
- //设置尺寸和位置
- edt_pwd->resize(280,45);
- edt_pwd->move(140,230);
- //设置默认值
- edt_pwd->setPlaceholderText("密码");
- //设置密码行为回显模式
- edt_pwd->setEchoMode(QLineEdit::Password); //设置密文模式
- //设置边框只留底行
- edt_pwd->setStyleSheet("border: none; border-bottom: 1px solid black;");
-
- //定义登录按钮
- QPushButton *btn_login = new QPushButton("登录",this);
- //设置登录图片
- btn_login->setIcon(QIcon(":/new/prefix1/icon/login.png"));
- //设置尺寸、位置和颜色
- btn_login->resize(80,40);
- btn_login->move(150,300);
- btn_login->setStyleSheet("background-color:rgb(19,183,246)");
- //定义登录按钮
- QPushButton *btn_cancel = new QPushButton("取消",this);
- //设置取消图片
- btn_cancel->setIcon(QIcon(":/new/prefix1/icon/cancel.png"));
- //设置尺寸、位置和颜色
- btn_cancel->resize(80,40);
- btn_cancel->move(310,300);
- btn_cancel->setStyleSheet("background-color:red");
- // 将登录按钮和取消按钮与相应的槽函数关联
- connect(btn_login, &QPushButton::clicked, this, &Widget::on_loginButton_clicked);
- connect(btn_cancel, &QPushButton::clicked, this, &Widget::on_cancelButton_clicked);
- }
-
- // 登录按钮被点击时执行的槽函数
- void Widget::on_loginButton_clicked()
- {
- //获取账号和密码输入框中的文本
- QString intputAccout=edt_user->text();
- QString intputPassword=edt_pwd->text();
- // 验证账号和密码是否匹配
- if (intputAccout == "admin" && intputPassword == "123456")
- {
- // 登录成功,弹出信息对话框并发射登录成功信号
- QMessageBox::information(this, "信息", "登录成功");
- emit loginSuccess();
- } else {
- // 登录失败,弹出错误对话框并询问是否重新登录
- QMessageBox::StandardButton result = QMessageBox::warning(this, "错误", "密码错误,请重试!", QMessageBox::Yes | QMessageBox::No);
-
- if (result == QMessageBox::Yes) {
- // 重新登录,清空密码输入框
- edt_pwd->clear();
- } else {
- // 不重新登录,关闭登录窗口
- close();
- }
- }
-
-
- }
- // 取消按钮被点击时执行的槽函数
- void Widget::on_cancelButton_clicked()
- {
- // 弹出问题对话框,询问是否确定要退出登录
- QMessageBox box(QMessageBox::Question, "问题", "是否退出?", QMessageBox::Yes | QMessageBox::No);
- box.setButtonText(QMessageBox::Yes, "Yes");
- box.setButtonText(QMessageBox::No, "No");
-
- // 将 Yes 和 No 按钮与相应的槽函数关联
- if (box.exec() == QMessageBox::Yes) {
- close();
- }
- }
-
-
- Widget::~Widget()
- {
- delete ui;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。