Qt实现带验证码的输入框

效果如图:

1、logonwidget.h文件

#ifndef LOGONWIDGET_H
#define LOGINWIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTimer>

class LogonWidget : public QWidget
{
    Q_OBJECT

public:
    explicit LogonWidget(QWidget *parent = nullptr);
    ~LogonWidget();
    QString GetUser();
    QString GetPwd();

protected:
    void paintEvent(QPaintEvent* evt);
    QString getCaptcha();
    Qt::GlobalColor* getColor();

private slots:
    void CancelBtn_Clicked();
    void LoginBtn_Clicked();
    void Timer_Timeout();

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;
};

#endif // LOGINWIDGET_H

2、logonwidget.cpp文件

#include "logonwidget.h"

#include <QPainter>
#include <QTime>
#include <QMessageBox>

LogonWidget::LogonWidget(QWidget *parent) :
    QWidget(parent),
    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);

    LoginBtn.setText("Login");
    LoginBtn.move(85, 140);
    LoginBtn.resize(85, 30);

    CancelBtn.setText("Reset");
    CancelBtn.move(180, 140);
    CancelBtn.resize(85, 30);

    m_timer.setParent(this);

    setWindowTitle("Login");
    setFixedSize(290, 190);

    connect(&LoginBtn, SIGNAL(clicked()), this, SLOT(LoginBtn_Clicked()));
    connect(&CancelBtn, SIGNAL(clicked()), this, SLOT(CancelBtn_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);
}

LogonWidget::~LogonWidget()
{
}

void LogonWidget::CancelBtn_Clicked()
{
//    done(Rejected);
}

void LogonWidget::LoginBtn_Clicked()
{
    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();
    }
}

void LogonWidget::Timer_Timeout()
{
    m_color = getColor();

    update();
}

QString LogonWidget::GetUser()
{
    return m_user;
}

QString LogonWidget::GetPwd()
{
    return m_pwd;
}

void LogonWidget::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 LogonWidget::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* LogonWidget::getColor()
{
    static Qt::GlobalColor colors[4];

    for(int i = 0; i < 4; i++)
    {
        colors[i] = static_cast<Qt::GlobalColor>((qrand() % 16) + 2);
    }
    return colors;
}
发布了34 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Sparrow_du/article/details/101027431
今日推荐