9_3_LineEdit
LineEdit
InputMask 版本
//核心属性
inputMask()//输⼊内容格式约束,参数"000",输入内容就限制只能输入三个数字
echoMode()//显⽰⽅式.参数有QLineEdit::Normal、QLineEdit::Password、QLineEdit::NoEcho
placeHolderText()//当输⼊框内容为空的时候, 显⽰什么样的提⽰信息,参数是字符串
clearButtonEnabled()//是否会⾃动显⽰出 "清除按钮,参数是布尔类型。
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//初始化第一个编译框,给它加上默认的提示语和一键清除按键
ui->lineEdit_name->setPlaceholderText("请输入名称");
ui->lineEdit_name->setClearButtonEnabled(true);
//初始化第二个编译框,给它加上默认的提示语和一键清除按键,给密码装上保护措施
ui->lineEdit_password->setPlaceholderText("请输入名称");
ui->lineEdit_password->setClearButtonEnabled(true);
ui->lineEdit_password->setEchoMode(QLineEdit::Password);
//初始化性别
ui->radioButton_male->setChecked(true);
//初始化第三个
ui->lineEdit_phoneNumber->setPlaceholderText("请输入电话");
ui->lineEdit_phoneNumber->setClearButtonEnabled(true);
ui->lineEdit_phoneNumber->setInputMask("00000000000");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QString gender = ui->radioButton_male->isEnabled()?"男":"女";
qDebug()<< "姓名:"<<ui->lineEdit_name->text()
<<"密码:"<<ui->lineEdit_password->text()
<<"性别:"<<gender
<<"手机号"<<ui->lineEdit_phoneNumber->text();
}
正则表达式版本
正则表达式:QRegExp (Regular Express)
//设置正则表达式,设置正则表达式需要的验证器
QRegExp regExp("^1\\d{10}$");
//需要给单行输入框设置一个验证器,这样下面才能对输入框中的文本进行验证
ui->lineEdit->setValidator(new QRegExpValidator(regExp));
//validate有两个返回值,一个是acceptable,一个是invalid,还一个忘了,参数是字符串和整形数字,整形没意义。
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->pushButton->setEnabled(false);
QRegExp regExp("^1\\d{10}$");
//需要给单行输入框设置一个验证器,这样下面才能对输入框中的文本进行验证
ui->lineEdit->setValidator(new QRegExpValidator(regExp));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_lineEdit_textEdited(const QString &text)
{
QString string = text;
int pos = 0;
if(ui->lineEdit->validator()->validate(string, pos) == QRegExpValidator::Acceptable)
{
ui->pushButton->setEnabled(true);
}
else
{
ui->pushButton->setEnabled(false);
}
}
对比两个密码
//判断字符串是否为空字符串,返回值是个布尔类型的
isEmpty();
void Widget::on_lineEdit_textEdited(const QString &arg1)
{
(void) arg1;
compare();
}
void Widget::compare()
{
const QString t1 = ui->lineEdit->text();
const QString t2 = ui->lineEdit_2->text();
if(t1.isEmpty() && t2.isEmpty())
{
ui->label->setText("密码为空");
}
else if(t1 == t2)
{
ui->label->setText("密码一致");
}
else
{
ui->label->setText("密码不一致");
}
}
void Widget::on_lineEdit_2_textEdited(const QString &arg1)
{
(void) arg1;
compare();
}
显示密码
插入个屁
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->label->setText("请输入密码");
ui->lineEdit->setPlaceholderText("第一次输入密码");
ui->lineEdit_2->setPlaceholderText("第二次输入密码");
ui->lineEdit->setEchoMode(QLineEdit::Password);
ui->lineEdit_2->setEchoMode(QLineEdit::Password);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_lineEdit_textEdited(const QString &arg1)
{
(void) arg1;
compare();
}
void Widget::compare()
{
const QString t1 = ui->lineEdit->text();
const QString t2 = ui->lineEdit_2->text();
if(t1.isEmpty() && t2.isEmpty())
{
ui->label->setText("密码为空");
}
else if(t1 == t2)
{
ui->label->setText("密码一致");
}
else
{
ui->label->setText("密码不一致");
}
}
void Widget::on_lineEdit_2_textEdited(const QString &arg1)
{
(void) arg1;
compare();
}
void Widget::on_checkBox_1_toggled(bool checked)
{
if(checked)
{
ui->lineEdit->setEchoMode(QLineEdit::Normal);
}
else{
ui->lineEdit->setEchoMode(QLineEdit::Password);
}
}
void Widget::on_checkBox_2_toggled(bool checked)
{
if(checked)
{
ui->lineEdit->setEchoMode(QLineEdit::Normal);
}
else{
ui->lineEdit->setEchoMode(QLineEdit::Password);
}
}