当前位置:网站首页>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();
}
进一步的开发
- 检查用户名和密码是否为空
- 当用户名或密码为空时提示错误
- 随机验证码
- 当验证码输入错误是进行提示
- 验证码随机刷新
小结
- 登录对话框作为可复用的软件部件进行开发
- 对话框之间通过成员变量和成员函数传递数据
- 将用户数据保存在私有成员变量中
- 通过公有函数进行数据传递
边栏推荐
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
- 编程内功之编程语言众多的原因
- [技术发展-24]:现有物联网通信技术特点
- Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
- There is nothing new under the sun. Can the meta universe go higher?
- The solution of Chinese font garbled code in keil5
- [développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
- Go language web development series 30: gin: grouping by version for routing
- Logback log sorting
猜你喜欢

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?

GoLand 2021.2 configure go (go1.17.6)

Richview trvstyle liststyle list style (bullet number)

Flutter动态化 | Fair 2.5.0 新版本特性

研发团队资源成本优化实践

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

Golang - command line tool Cobra

SQL Injection (AJAX/JSON/jQuery)

Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock

Internet of things completion -- (stm32f407 connects to cloud platform detection data)
随机推荐
Richview trvstyle liststyle list style (bullet number)
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Father and basketball
GoLand 2021.2 configure go (go1.17.6)
编程内功之编程语言众多的原因
软件测试工作那么难找,只有外包offer,我该去么?
Unity render streaming communicates with unity through JS
Use and design of Muduo buffer class
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
使用vscode查看Hex或UTF-8编码
Leetcode-1175. Prime Arrangements
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
Flutter动态化 | Fair 2.5.0 新版本特性
[technology development-24]: characteristics of existing IOT communication technology
Conversion function and explicit
[redis] cache warm-up, cache avalanche and cache breakdown
Golang — template
Flutter dynamic | fair 2.5.0 new version features
SQL Injection (GET/Search)
mysql中的字段问题