赞
踩
#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(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。