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简单登录对话框(带验证码功能)的更多相关文章

  1. 一个基于QT简单登录对话框

    1. 登录对话框式样 2. QLoginDialog.h #ifndef DIALOG_H #define DIALOG_H #include <QtGui/QDialog> #inclu ...

  2. artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口

    artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...

  3. CountBoard 是一个基于Tkinter简单的,开源的桌面日程倒计时应用

    CountBoard 是一个基于Tkinter简单的,开源的桌面日程倒计时应用. 项目地址 https://github.com/Gaoyongxian666/CountBoard 基本功能 置顶功能 ...

  4. spring boot:spring security给用户登录增加自动登录及图形验证码功能(spring boot 2.3.1)

    一,图形验证码的用途? 1,什么是图形验证码? 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers ...

  5. 基于bootstrap表单登录(带验证码)

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!-- ...

  6. 一个基于Qt的截屏程序

    最近有一个arm板上的程序需要重写用户手册,在网上找了许久,没找到合适的截屏工具.于是只好自己动手做一个了. 因为arm板上有已经有了Qt环境,于是想到用 Qt的QPixmap::grabWindow ...

  7. 基于web的网上书城系统开发-----登录注册扩展-------验证码功能

    public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; privat ...

  8. qt 简单登录界面(一)

    widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include<QLineEdit> class ...

  9. jQuery与vue分别实现超级简单的绿色拖动验证码功能

    jquery的绿色拖动验证功能 在网上看到了一个这样的问题:那种像拖动滑块匹配图形的验证方式是怎么实现的?. 突然想到实现一个简单绿色拖动验证码的功能,在网上搜了下,有一个用jquery实现的该功能代 ...

随机推荐

  1. 百度Apollo解析——2.log系统

    Apollo中的glog 在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善. 1. 安装 以下是官方 ...

  2. 一堵墙IFC数据-wall.ifc

    这是一面墙的IFC数据内容 =====================================文档内容======================================= ISO-1 ...

  3. 30-盐水(分段dfs)

    链接:https://www.nowcoder.com/acm/contest/94/K来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 131072K,其他语言2621 ...

  4. Luogu 3586 [POI2015]LOG

    考虑离散化后开权值线段树. 设序列中不小于$s$的数有$cnt$个,小于$s$的数的和为$sum$. 那么操作Z能成功的充要条件是$sum \geq (c - cnt) * s$. 如果序列中不小于$ ...

  5. Xshell连接linux(deepin)时提示ssh服务器拒绝了密码,请再试一次解决方法

    用Xshell root连接时显示ssh服务器拒绝了密码,应该是应该是sshd的设置不允许root用户用密码远程登录 修改 /etc/ssh/sshd_config文件,注意,安装了openssh才会 ...

  6. 阿里云ECS centos7.2 支持IPv6

    公司的项目因为服务器没有支持IPv6而被appstore给退回来了 第一部分 第一步:编辑 /etc/sysctl.conf 文件,将其中三条禁用IPv6的设置更改为: 第二步:使用命令启动启用IPv ...

  7. Notepad++ 代码格式化

    在阅读别人的代码时偶尔会遇到格式很乱,阅读起来很费劲的情况,若手动改,很容易出错且很费时间,这时可以借助一些专业的编辑器来格式化代码,NotePad++是一个轻量级的代码编辑器,占用内存少,运行速度快 ...

  8. Android手游外挂入侵----寓攻于守,方能破敌

    欢迎访问网易云社区,了解更多网易技术产品运营经验. 手游外挂入侵 随着各种爆款手游的风靡,目前手机游戏的占比用户已经形成一个巨大的市场,市场上你争我夺,有将PC版本移植到手机中,也有新模式手游的推出. ...

  9. 正经学C#_判断[IF语句]:[c#入门经典]

    判断语句几乎是最为常用的语句之一,是最有效的. 先说IF语句,IF语句也是属于分支的一种,用来控制流程的. IF的语句是这样的 IF(xxx 条件) { //代码块 } ,b; ) { b=a--; ...

  10. java项目中获取文件路径的几种方法

    // 第一种: 2 File f = new File(this.getClass().getResource("/").getPath()); // 结果: /Users/adm ...