我正在使用一本名为" c ++编程gui和qt 4 second edition"的书中的一个例子,并遇到了以下问题;我无法编辑QlineEdit。我很确定导致问题的是QRegExp,因为当我评论它时,我突然能够在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();
}
答案 0 :(得分:0)
关于regExp的声明应该是:
QRegExp regExp = ("[A-Za-z] [1-9] [0-9] {0,2}")
{0,2}应该有逗号而不是连字符( - )