正则表达式的应用&按钮可执行状态的改变

    xiaoxiao2021-03-25  99

    题目:根据lineEdit中输入的表达式是否有效来启用或者停用“OK”按钮。

    1、新建一个基类为Dialog(可选)的Qt工程; 2、界面:

    3、设置widget的属性 *选择Label,设置 objectName 属性为“label”,并且设置text属性为“&CellLocation” *选择Line Edit,设置objectName属性为“lineEdit” *选择第一个按钮,将其objectName属性设置为“ok_pushbutton”,enabled属性设置为“false”,text属性设置为“OK”,并将default属性设置为“true” *选择第二个按钮,设置objectName属性为“cancel——pushbutton”,将text设置为“cancel” *设置表单背景window Title属性为“Go To Cell” 4、运行,label会显示一个“&”,为了解决这个问题,选择“Edit Buddy(编辑伙伴)”,在这个命令下,选中label并拖曳至lineEdit,然后放开,使得label和Line Edit成为伙伴关系。 5、代码

    #include "dialog.h" #include "ui_dialog.h" #include <QRegExp> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); QRegExp m_regExp("[A-Za-z][1-9][0-9]{0,2}"); //正则表达式 ui->lineEdit->setValidator(new QRegExpValidator(m_regExp)); //正则表达式验证器 } Dialog::~Dialog() { delete ui; } void Dialog::on_lineEdit_textChanged(const QString &arg1) { ui->ok_pushButton->setEnabled(ui->lineEdit->hasAcceptableInput()); } void Dialog::on_cancel_pushButton_clicked() { this->close(); } void Dialog::on_ok_pushButton_clicked() { this->hide(); }

    6、运行结果

    转载请注明原文地址: https://ju.6miu.com/read-25427.html

    最新回复(0)