无法编辑QlineEdit
问题描述:
我正在使用一本名为“C++编程与qt 4第二版gui”的书中的例子,并遇到以下问题;我无法编辑QlineEdit。我很确定这是导致问题的QRegExp,因为当我评论它时,我突然能够输入到QlineEdit对话框中。无法编辑QlineEdit
这里是下面的代码:
cells.h:
#ifndef CELLS
#define CELLS
#include <QDialog>
#include "ui_cells.h"
class cells: public QDialog, public Ui::cells
{
Q_OBJECT
public:
cells(QWidget *parent = 0);
private slots:
void on_lineEdit_textChanged();
};
#endif // CELLS
cells.cpp:
#include <QtWidgets>
#include "cells.h"
cells::cells(QWidget *parent) : QDialog(parent)
{
setupUi(this);
QRegExp regExp("[A-Za-a] [1-9] [0-9] {0-2}");
lineEdit->setValidator(new QRegExpValidator(regExp, this));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(Cancel, SIGNAL(clicked()), this, SLOT(reject()));
}
void cells::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
最后的main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "cells.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cells *dialog = new cells;
dialog->show();
return a.exec();
}
答
抱歉,想通了问题:
对正则表达式的声明应该是:
QRegExp regExp = ("[A-Za-z] [1-9] [0-9] {0,2}")
有应该是在{0,2}逗号不是一个连字符( - )