一个基于QT简单登录对话框(带验证码功能)
1. 对话框样式

2. 源代码
①. main.cpp
#include <QtGui/QApplication> #include "QLoginDialog.h"
#include "Widget.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv); Widget w;
w.show(); return a.exec();
}
②. Widget.h
#ifndef WIDEGT_H
#define WIDEGT_H #include <QWidget>
#include <QPushButton> class Widget : public QWidget
{
Q_OBJECT
private:
QPushButton testBtn;
private slots:
void TestBtn_Clicked();
public:
Widget(QWidget* parent = 0);
~Widget();
}; #endif // WIDEGT_H
③. Widget.cpp
#include <QDebug>
#include "Widget.h"
#include "QLoginDialog.h" Widget::Widget(QWidget* parent) : QWidget(parent) ,testBtn(this)
{
testBtn.setText("Test Login Dialog"); setFixedSize(200, 100); connect(&testBtn, SIGNAL(clicked()), this, SLOT(TestBtn_Clicked()));
}
void Widget::TestBtn_Clicked()
{
QLoginDialog dialog; if(dialog.exec() == QDialog::Accepted)
{
qDebug() << "User Name: " + dialog.GetUser();
qDebug() << "Password: " + dialog.GetPwd();
}
} Widget::~Widget()
{ }
④. QLoginDialog.h
#ifndef DIALOG_H
#define DIALOG_H #include <QtGui/QDialog>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTimer> class QLoginDialog : public QDialog
{
Q_OBJECT
private:
QLabel UserLable;
QLabel PwdLable;
QLabel CaptLable;
QPushButton CancelBtn;
QPushButton LoginBtn;
QLineEdit UserLineEdit;
QLineEdit PwdLineEdit;
QLineEdit CaptEdit;
QString m_user;
QString m_pwd;
QString m_captcha;
Qt::GlobalColor* m_color;
QTimer m_timer;
private slots:
void CancelBtn_Clicked();
void LoginBtn_Clicked();
void Timer_Timeout();
protected:
void paintEvent(QPaintEvent* evt);
QString getCaptcha();
Qt::GlobalColor* getColor();
public:
QLoginDialog(QWidget *parent = 0);
QString GetUser();
QString GetPwd();
~QLoginDialog();
}; #endif // DIALOG_H
⑤. QLoginDialog.cpp
#include <QDebug>
#include <QPainter>
#include <QTime>
#include <QMessageBox> #include "QLoginDialog.h" QLoginDialog::QLoginDialog(QWidget *parent) : QDialog(parent, Qt::WindowCloseButtonHint),
UserLable(this), PwdLable(this), CaptLable(this), CancelBtn(this), LoginBtn(this),
UserLineEdit(this), PwdLineEdit(this), CaptEdit(this)
{
UserLable.setText("User Name");
UserLable.move(20, 30);
UserLable.resize(60, 25); UserLineEdit.move(85, 30);
UserLineEdit.resize(180, 25); PwdLable.setText("Password");
PwdLable.move(20, 65);
PwdLable.resize(60, 25); PwdLineEdit.move(85, 65);
PwdLineEdit.resize(180, 25);
PwdLineEdit.setEchoMode(QLineEdit::Password); CaptLable.setText("Captcha");
CaptLable.move(20, 100);
CaptLable.resize(60, 25); CaptEdit.move(85, 100);
CaptEdit.resize(85, 25); CancelBtn.setText("Cancel");
CancelBtn.move(85, 140);
CancelBtn.resize(85, 30); LoginBtn.setText("Login");
LoginBtn.move(180, 140);
LoginBtn.resize(85, 30); m_timer.setParent(this); setWindowTitle("Login");
setFixedSize(290, 190); connect(&CancelBtn, SIGNAL(clicked()), this, SLOT(CancelBtn_Clicked()));
connect(&LoginBtn, SIGNAL(clicked()), this, SLOT(LoginBtn_Clicked())); connect(&m_timer, SIGNAL(timeout()), this, SLOT(Timer_Timeout())); qsrand(QTime::currentTime().second() * 1000 + QTime::currentTime().msec()); m_captcha = getCaptcha();
m_color = getColor(); m_timer.start(200);
} void QLoginDialog::CancelBtn_Clicked()
{
qDebug("CancelBtn_Clicked start"); done(Rejected); qDebug("CancelBtn_Clicked end");
} void QLoginDialog::LoginBtn_Clicked()
{
qDebug("LoginBtn_Clicked start"); m_user = UserLineEdit.text().trimmed();//trimmed():Delete space
m_pwd = PwdLineEdit.text(); QString captcha = CaptEdit.text().replace(" ", ""); if(captcha.toLower() == m_captcha.toLower())
{
if(m_user.isEmpty())
{
QMessageBox::information(this, "Info", "User ID can not be empty");
m_captcha = getCaptcha();
}
else if(m_pwd.isEmpty())
{
QMessageBox::information(this, "Info", "Password can not be empty");
m_captcha = getCaptcha();
}
else
{
done(Accepted);
}
}
else
{
QMessageBox::warning(this, "Warning", "Captcha is not macthed"); m_captcha = getCaptcha(); // CaptEdit.selectAll();
}
qDebug("LoginBtn_Clicked end");
} void QLoginDialog::Timer_Timeout()
{
m_color = getColor(); update();
} QString QLoginDialog::GetUser()
{
return m_user;
} QString QLoginDialog::GetPwd()
{
return m_pwd;
} void QLoginDialog::paintEvent(QPaintEvent *evt)
{
QPainter painter(this); painter.fillRect(180, 100, 84, 24, Qt::white); painter.setFont(QFont("Comic Sans MS")); for(int i = 0; i < 100; i++)
{
painter.setPen(m_color[i % 4]);
painter.drawPoint(180 + (qrand() % 84), 100 + (qrand() % 24));
} for(int i = 0; i < 4; i++)
{
painter.setPen(m_color[i]);
painter.drawText(180 + 20 * i, 100, 20, 24, Qt::AlignCenter, QString(m_captcha[i]));
}
} QString QLoginDialog::getCaptcha()
{
QString ret = ""; for(int i = 0; i < 4; i++)
{
int c = (qrand() % 2) ? 'a' : 'A'; ret += static_cast<QChar>(c + qrand() % 26);
} return ret;
} Qt::GlobalColor* QLoginDialog::getColor()
{
static Qt::GlobalColor colors[4]; for(int i = 0; i < 4; i++)
{
colors[i] = static_cast<Qt::GlobalColor>((qrand() % 16) + 2);
} return colors;
} QLoginDialog::~QLoginDialog()
{ }
一个基于QT简单登录对话框(带验证码功能)的更多相关文章
- 一个基于QT简单登录对话框
1. 登录对话框式样 2. QLoginDialog.h #ifndef DIALOG_H #define DIALOG_H #include <QtGui/QDialog> #inclu ...
- artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...
- CountBoard 是一个基于Tkinter简单的,开源的桌面日程倒计时应用
CountBoard 是一个基于Tkinter简单的,开源的桌面日程倒计时应用. 项目地址 https://github.com/Gaoyongxian666/CountBoard 基本功能 置顶功能 ...
- spring boot:spring security给用户登录增加自动登录及图形验证码功能(spring boot 2.3.1)
一,图形验证码的用途? 1,什么是图形验证码? 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers ...
- 基于bootstrap表单登录(带验证码)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!-- ...
- 一个基于Qt的截屏程序
最近有一个arm板上的程序需要重写用户手册,在网上找了许久,没找到合适的截屏工具.于是只好自己动手做一个了. 因为arm板上有已经有了Qt环境,于是想到用 Qt的QPixmap::grabWindow ...
- 基于web的网上书城系统开发-----登录注册扩展-------验证码功能
public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; privat ...
- qt 简单登录界面(一)
widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include<QLineEdit> class ...
- jQuery与vue分别实现超级简单的绿色拖动验证码功能
jquery的绿色拖动验证功能 在网上看到了一个这样的问题:那种像拖动滑块匹配图形的验证方式是怎么实现的?. 突然想到实现一个简单绿色拖动验证码的功能,在网上搜了下,有一个用jquery实现的该功能代 ...
随机推荐
- 连接ORACLE客户端工具navicat111.12 for oracle
安装navicat111.12 for oracle后 打开
- day70 12-存储过程和存储函数
什么是相关子查询? 这是一个子查询,子查询本身又是一个多表查询.where不能用组函数,但是可以用字符函数instr().除了order by排序没有考,查询语句的所有内容都考了.这个题有点难度. 今 ...
- day70-oracle 12-Java调用存储过程和存储函数
我们现在调用的是存储过程和存储函数.用CallableSatement调用存储函数和存储过程. RDBMS:关系数据库.使用标准方式调用存储过程.也就是说:在mysql中调用和在oracle中调用的写 ...
- intellij idea 在执行maven的操作 install等会出现中文乱码?其他程序打印正常?
之前一直碰到过这个问题,也没在意,因为那个中文对我来说用处不大,今天看着务必难受,一定把他给解决了,查了一下,找到了解决方法,如下: 首先打开你的设置. Setting->maven->r ...
- STM32 C++编程 003 USART(串口)类
使用 C++ 语言给 STM32 编写一个 Usart 类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编程 的 ...
- c语言实战: 计算时间差
计算时间差有两种,一种是把时间都转化为分钟数,一种是把时间都转化为小时,后者是会用到除法所以不可避免产生浮点数,所以我们选择转化为分钟数来计算. //题目:给定两个时间点计算它们的时间差,比如,1:5 ...
- HBase 协处理器统计行数
环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...
- Blocks UVA - 10559
传送门 题目大意 有n个带有颜色的方块,没消除一段长度为x的连续的相同颜色的方块可以得到x^2的分数,让你用一种最优的顺序消除所有方块使得得分最多. 分析 首先不难看出这是一个区间dp,于是我们考虑如 ...
- linux ftp、sftp、telnet服务开通、更改Orale最大连接数
1 ftp服务开通 1.1 检测vsftpd是否安装及启动 先用service vsftpd status 来查看ftp是否开启.也可以使用ps -ef | grep ftp 来查看本地是否含有包含f ...
- format Code
setting中设置format code. 方便格式化代码.