我使用了一本名为"c++ programming a gui with qt4 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 // CELLScells.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();
}发布于 2015-08-08 23:31:11
抱歉,我想通了:
在regExp的声明中,应该是:
QRegExp regExp = ("[A-Za-z] [1-9] [0-9] {0,2}"){0,2}处应该有一个逗号,而不是连字符(-)
https://stackoverflow.com/questions/31895482
复制相似问题