无法编辑QlineEdit

unable to edit QlineEdit

本文关键字:QlineEdit 编辑      更新时间:2023-10-16

我正在使用一本名为《c++编程gui与qt 4第二版》的书中的一个例子,并遇到了以下问题;我不能编辑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();
    }

对不起,找到问题了:

regExp的声明应该是:

    QRegExp regExp = ("[A-Za-z] [1-9] [0-9] {0,2}")

{0,2}应该是逗号,而不是连字符(-)