一个基于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实现的该功能代 ...
随机推荐
- android-auto-scroll-view-pager (无限广告轮播图)
github 地址: https://github.com/Trinea/android-auto-scroll-view-pager Gradle: compile ('cn.trinea.andr ...
- sql基本查询语句练习
student(S#,Sname,Sage,Ssex) 学生表 S#:学号: Sname:学生姓名:Sage:学生年龄:Ssex:学生性别 Course(C#,Cname,T#) 课程表 ...
- linux终端后台运行
nohup command &(然后X退出即可) &也可用来在终端中同时执行几条命令(并行,最后面不要忘记加&) command1 & command2 & c ...
- [xdoj1007]易碎的鸟蛋(dp)
解题思路:f[n,m]表示n层楼.m个鸡蛋时所需要的最小次数,则 转移方程为:f[n,m] = min{ 1+max(f[i-1,m-1], f[n-i,m]) | i=1..n }初始条件:f[i, ...
- LINUX关闭防火墙、开放特定端口等常用操作
1. 重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效: 开启:service iptables s ...
- 2018多校第九场1004(HDU 6415) DP
本以为是个找规律的题一直没找出来... 题目:给你一个n*m的矩阵和1-n*m个数,问有多少种情况满足纳什均衡的点只有一个.纳什均衡点是指这个元素在所在行和所在列都是最大的. 思路:吉老师直播的思路: ...
- [转]SQL 模糊查询
在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表 ...
- Linux脚本设计4——一些实用程序
实用程序1:列目录 #!/bin/bash path=`echo $PATH | sed 's/:/ /g'` for d in $path do echo $d done 这是一个for循环,注意p ...
- 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(四)
ViewPagerIndicator+ViewPager 要想使用ViewPagerIndicator,要使用到viewPagerlibrary开源库 top.xml <?xml version ...
- C#多线程 线程嵌套调用问题
线程嵌套指的是:线程A的执行代码启动了线程B,线程B的执行代码又启动了线程C. 我原本以为线程A被Abort后,线程B会自动被Abort,但是我大错特错了. 在这种场景下,线程的管理就非常重要了. 线 ...