当前位置:网站首页>Qt学习18 登录对话框实例分析
Qt学习18 登录对话框实例分析
2022-07-03 13:23:00 【一个小黑酱】
Qt学习18 登录对话框实例分析
登录对话框是应用程序中的常用部件
思考:如何开发一个可以在不同项目间复用的登录对话框?
登录对话框需求分析
- 可复用软件部件
- 获取用户名和密码
附加需求
- 随机验证码
登录对话框的设计与架构

问题
- 如何获取用户输入的用户名和密码?
- 如何在两个不同的对话框见传递数据?

通过附加的成员变量和成员函数,完成不同对话框见的数据传递
代码展示
#ifndef QLOGINDIALOG_H
#define QLOGINDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class QLoginDialog : public QDialog
{
Q_OBJECT
private:
QLabel UserLabel;
QLabel PwdLabel;
QLineEdit UserEdit;
QLineEdit PwdEdit;
QPushButton LoginBtn;
QPushButton CancelBtn;
QString m_User;
QString m_Pwd;
private slots:
void LoginBtn_Clicked();
void CancelBtn_Clicked();
public:
QLoginDialog(QWidget *parent = 0);
~QLoginDialog();
QString getUser();
QString getPwd();
};
#endif // QLOGINDIALOG_H
#include "QLoginDialog.h"
#include <QDebug>
QLoginDialog::QLoginDialog(QWidget *parent) : QDialog(parent, Qt::WindowCloseButtonHint),
UserLabel(this), PwdLabel(this), UserEdit(this), PwdEdit(this), LoginBtn(this), CancelBtn(this)
{
UserLabel.setText("User ID:");
UserLabel.move(20, 30);
UserLabel.resize(60, 25);
UserEdit.move(85, 30);
UserEdit.resize(180, 25);
PwdLabel.setText("Password:");
PwdLabel.move(20, 65);
PwdLabel.resize(60, 25);
PwdEdit.move(85, 65);
PwdEdit.resize(180, 25);
PwdEdit.setEchoMode(QLineEdit::Password);
CancelBtn.setText("Cancel");
CancelBtn.move(85, 110);
CancelBtn.resize(85, 30);
LoginBtn.setText("Login");
LoginBtn.move(180, 110);
LoginBtn.resize(85, 30);
setWindowTitle("Login");
setFixedSize(285, 170);
connect(&LoginBtn, SIGNAL(clicked()), this, SLOT(LoginBtn_Clicked()));
connect(&CancelBtn, SIGNAL(clicked()), this, SLOT(CancelBtn_Clicked()));
}
QLoginDialog::~QLoginDialog()
{
}
QString QLoginDialog::getUser()
{
return m_User;
}
QString QLoginDialog::getPwd()
{
return m_Pwd;
}
void QLoginDialog::LoginBtn_Clicked()
{
qDebug() << "LoginBtn_Clicked() begin";
m_User = UserEdit.text().trimmed();
m_Pwd = PwdEdit.text();
done(Accepted);
qDebug() << "LoginBtn_Clicked() end";
}
void QLoginDialog::CancelBtn_Clicked()
{
qDebug() << "CancelBtn_Clicked() begin";
done(Rejected);
qDebug() << "CancelBtn_Clicked() end";
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton TestBtn;
private slots:
void TestBtn_Clicked();
public:
explicit Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
#include "Widget.h"
#include "QLoginDialog.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent), TestBtn(this)
{
TestBtn.setText("Test Login Dialog");
setFixedSize(200, 50);
connect(&TestBtn, SIGNAL(clicked()), this, SLOT(TestBtn_Clicked()));
}
Widget::~Widget()
{
}
void Widget::TestBtn_Clicked()
{
QLoginDialog dlg;
if (dlg.exec() == QDialog::Accepted) {
qDebug() << "Accepted";
qDebug() << "username:" << dlg.getUser();
qDebug() << "password:" << dlg.getPwd();
}
}
#include <QApplication>
#include "Widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
进一步的开发
- 检查用户名和密码是否为空
- 当用户名或密码为空时提示错误
- 随机验证码
- 当验证码输入错误是进行提示
- 验证码随机刷新
小结
- 登录对话框作为可复用的软件部件进行开发
- 对话框之间通过成员变量和成员函数传递数据
- 将用户数据保存在私有成员变量中
- 通过公有函数进行数据传递
边栏推荐
- SQL Injection (AJAX/JSON/jQuery)
- Brief analysis of tensorboard visual processing cases
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- 常见的几种最优化方法Matlab原理和深度分析
- [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
- Libuv Library - Design Overview (Chinese version)
- JVM系列——概述,程序计数器day1-1
- Flutter动态化 | Fair 2.5.0 新版本特性
- Replace the GPU card number when pytorch loads the historical model, map_ Location settings
- [bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
猜你喜欢

MySQL functions and related cases and exercises

MySQL 数据处理值增删改

Go language web development series 30: gin: grouping by version for routing

Go language unit test 4: go language uses gomonkey to test functions or methods

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off

3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!

There is nothing new under the sun. Can the meta universe go higher?
随机推荐
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
Golang — template
常见的几种最优化方法Matlab原理和深度分析
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
Golang - command line tool Cobra
php 迷宫游戏
Unity render streaming communicates with unity through JS
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
[技術發展-24]:現有物聯網通信技術特點
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
软件测试工作那么难找,只有外包offer,我该去么?
如何使用lxml判断网站公告是否更新
Record 405 questions about bank callback post request
PhpMyAdmin stage file contains analysis traceability
8 Queen question
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
JS convert pseudo array to array
Using registered classes to realize specific type matching function template