QLineEdit和QPushButton实现了输入用户名、密码并验证的功能

使用QLineEdit和QPushButton实现了输入用户名、密码并验证的功能。该程序使用正则表达式限制了用户名和密码只能包含数字、字母和下划线,且长度在4到16个字符之间。如果输入的用户名和密码符合要求,则弹出一个消息框显示“登录成功”,否则弹出一个消息框显示“登录失败”。

#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include <QRegExpValidator>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget w;
    w.resize(300, 150);

    QLineEdit userLineEdit(&w);
    userLineEdit.setPlaceholderText("请输入用户名");
    userLineEdit.setGeometry(50, 30, 200, 30);
    QRegExp userRegExp("[A-Za-z0-9_]{4,16}");
    userLineEdit.setValidator(new QRegExpValidator(userRegExp, &userLineEdit));

    QLineEdit passwordLineEdit(&w);
    passwordLineEdit.setPlaceholderText("请输入密码");
    passwordLineEdit.setGeometry(50, 70, 200, 30);
    passwordLineEdit.setEchoMode(QLineEdit::Password);
    //限制输入中文和中文字符
    //QRegExp passwordRegExp("^[A-Za-z0-9`~!@#$%^&*()_-+=<>,.\\\/]+$") 
    QRegExp passwordRegExp("[A-Za-z0-9_]{4,16}");
    passwordLineEdit.setValidator(new QRegExpValidator(passwordRegExp, &passwordLineEdit));

    QPushButton loginButton("登录", &w);
    loginButton.setGeometry(100, 110, 100, 30);
    QObject::connect(&loginButton, &QPushButton::clicked, [&]() {
        QString username = userLineEdit.text();
        QString password = passwordLineEdit.text();
        if (userLineEdit.hasAcceptableInput() && passwordLineEdit.hasAcceptableInput()) {
            if (username == "admin" && password == "123456") {
                QMessageBox::information(&w, "登录成功", "欢迎您,管理员!");
            } else {
                QMessageBox::warning(&w, "登录失败", "用户名或密码错误,请重试!");
            }
        } else {
            QMessageBox::warning(&w, "输入错误", "用户名或密码格式错误,请重新输入!");
        }
        //判断QLineEdit内容是否合法
        //bool wifiPassState;
    	//QRegExp regx("^[A-Za-z0-9`~!@#$%^&*()_-+=<>,.\\\/]+$");//禁止输入中文字符	
    	//wifiPassState = regx.exactMatch(ui.passwordLineEdit.text());
    	//if(wifiPassState)
    	//{
	    //   	 qDebug() << QStringLiteral("wifiPassword格式正确!");
	    //}
    	//else
    	//{
        //	qDebug() << QStringLiteral("wifiPassword格式错误!");
        //	QMessageBox::warning(this, "输入错误", "WiFi密码禁止中文,请重新输入!");
        //	return ;
   	 	//}
        
    });

    w.show();

    return a.exec();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值