一个基于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实现的该功能代 ...
随机推荐
- 为SSRS配置SMTP服务器身份验证
此处设置外邮地址却无法填写邮箱密码 一.安装SMTP服务 1.在服务管理器中单击“功能” 2.单击“添加功能”打开“添加功能向导”对话框 3.在“选择功能”页上选择“SMTP服务器”并选择“添加必须的 ...
- JVM实用参数(三)打印所有XX参数及值
JVM实用参数(三)打印所有XX参数及值 原文地址:https://blog.codecentric.de/en/2012/07/useful-jvm-flags-part-3-printing-al ...
- 恢复oracle的回收站的所有的表
使用sys as sysdba 进入到sqlplus的控制界面 sqlplus / as sysdba 执行相关的命令,自动生成一个脚本文件 spool d:/a.sql select 'flashb ...
- SQl Server 函数篇 聚合函数
说一下数据库中的聚合函数 函数使用必须加小括号(), 5种聚合函数: 1.max最大值 select max(price) from car where code='c024' --取这一列中 ...
- Luogu 3822 [NOI2017]整数
看懂了的大佬的题解.(这个id太巨了,找不到他的blog) 考虑直接暴力算进位均摊复杂度是对的,证明戳这里. 但是题目要求我们支持一个减操作,这就相当于返回之前操作前的结果,这对于这种均摊的复杂度的东 ...
- Entity Framework Tutorial Basics(15):Querying with EDM
Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...
- web.xml中url-pattern匹配规则.RP
一.url-pattern的三种写法 精确匹配.以"/"开头,加上servlet名称. /ad 路径匹配.以"/"开头,加上通配符"*" ...
- 选择性搜索(Selective Search)
1 概述 本文牵涉的概念是候选区域(Region Proposal ),用于物体检测算法的输入.无论是机器学习算法还是深度学习算法,候选区域都有用武之地. 2 物体检测和物体识别 物体识别是要分辨出图 ...
- spark(1) - ubuntu 下 spark 安装
简单步骤: 前提:hadoop 环境搭建(我的是伪分布式) 1.官网下载spark 2.spark部署(单机模式): (1)解压 (2)移动文件到自定义目录下(同时修改文件名-原来的名字太长) (3) ...
- CHTools-Swift版本目录介绍
CHSwiftBase CHViewControllers CHUI类 CHNetRequest CHSaveData CHSpecialEffect(特效) Other CHSwiftPCH 全局常 ...