1、在头文件的类声明中加入槽
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
private slots://输入内容改变槽
void on_lineEdit_textChanged();
};
#endif // DIALOG_H
2、在源文件中构造函数中添加代码
#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-9]{0,2}");
//只允许第一个字元输入大小写英文字母,后面接一位非零的数字,再接0-2位可为0的数字
ui->lineEdit->setValidator(new QRegExpValidator(regExp,this));
connect(ui->okButton,SIGNAL(clicked()),this,SLOT(accept()));
connect(ui->cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
}
//实现槽函数,根据lineEdit中输入的文字是否有效来启用或停用OK按钮,
void Dialog::on_lineEdit_textChanged()
{
ui->okButton->setEnabled(ui->lineEdit->hasAcceptableInput());
}
Dialog::~Dialog()
{
delete ui;
}