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.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
QRegExp regExp("[A-Za-z][1-9]{0,2}");
ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));
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();
}