一个基于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实现的该功能代 ...
随机推荐
- viewpagerindicator+UnderlinePageIndicator+ viewpage切换
布局文件activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- 624. Maximum Distance in Arrays二重数组中的最大差值距离
[抄题]: Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers ...
- Mind Map - FreeMind
FreeMind[1]是一款基于java的免费的脑图(mind mapping)制作与管理软件.FreeMind开发项目组正致力于使其成为一款高效率的工具.FreeMind具有一键“展开/折叠”功能以 ...
- c语言实战: 计算时间差
计算时间差有两种,一种是把时间都转化为分钟数,一种是把时间都转化为小时,后者是会用到除法所以不可避免产生浮点数,所以我们选择转化为分钟数来计算. //题目:给定两个时间点计算它们的时间差,比如,1:5 ...
- zookeeper生成节点、删除节点 For Java
源码地址https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zooke ...
- C++文件流打开标识符.RP
ofstream流,以ios::app打开(或者“ios::app|ios::out”),如果没有文件,那么生成空文件:如果有文件,那么在文件尾追加.以ios::app|ios::in打开,不管有没有 ...
- c++中怎么实现Java中finally语句
所有学习c++的书籍都明确提出了,不要使用goto, 以免造成程序流程的混乱,使理解和调试程序都产生困难. 但是我们遇到这样一个场景怎么办:就是不管程序执行成功与否,都要执行一些资源释放语句,相当ja ...
- c# 使用protobuf格式操作 Redis
protobuf格式介绍 1.protobuf为goole定义的类似于json的数据格式.2.最终都需要序列化为二进制形式进行传输存储.3.相对于xml,json格式来说,序列化为二进制后占用空间更小 ...
- const 在C++中的区别
一.Const作用 如下表所示: No. 作用 说明 参考代码 1 可以定义const常量 const int Max = 100; 2 便于进行类型检查 const常量有数据类型,而宏常量没有 ...
- 原型模式与serializable
写原型模式时课件上有一个实现模式是利用可串行化接口实现,然后就发现那个代码(如下),串行化接口里面没有函数,这种接口被曾为标记接口,implements这个接口后就可以对其进行各种流操作了,其实就是O ...