qt-03控件正则绑定-dialog退出获取代码-编辑伙伴

控件正则绑定-dialog退出获取代码-编辑伙伴

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QRegExpValidator>


QT_BEGIN_NAMESPACE
namespace Ui {
class Dialog;
}
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;
private slots:
    void on_lineEdit_textChanged();
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    //正则表达式  第一个字母为a-z大小写,第二个为数字 不能为0;第三 后面可以跟2个数字 0-9;
    QRegExp regExp("[A-Za-z][1-9]{0,2}");
    //指定objectname = lineEdit 正则用在lineEdit 界面的输入控件
    ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));
    //dialog 设置对象退出的代码  例如:    w.show();
    /*
    if(w.exec() == QDialog::Rejected)
    {

        return a.exec();
    }
   */
    connect(ui->OKbutton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_lineEdit_textChanged()
{
    ui->OKbutton->setEnabled(ui->lineEdit->hasAcceptableInput());



}

main.cpp

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    if(w.exec() == QDialog::Rejected)
    {

       return a.exec();
    }
    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值