一个基于QT简单登录对话框(带验证码功能)
Posted 99度的水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个基于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简单登录对话框(带验证码功能)的主要内容,如果未能解决你的问题,请参考以下文章