Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to apply validation on cells in a QTableWidget?
Forum Updated to NodeBB v4.3 + New Features

How to apply validation on cells in a QTableWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 5 Posters 15.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    Hi,

    Use the doc Luke.

    More seriously, the example linked can server as a base for number 2.
    And for number 1, return a QLineEdit with a regular expression validator that allows the chars you want.

    GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on last edited by VRonin
    #8

    @SGaist said in How to apply validation on cells in a QTableWidget?:

    QLineEdit

    For 1 here is what I have so far:

    void MainWindow::addSub(int row, int col)
    {
        auto *edit = new QLineEdit;
        QRegExp regExp("/^[^'\"]*$/");
        auto *validator = new QRegExpValidator(regExp, this);
        edit->setValidator(validator);
        auto item = new QTableWidgetItem();
        ui->tableWidget->setItem(row, col, item);
    }
    
    void MainWindow::addRow(int &last)
    {
        last = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(last);
        addSub(last, 0);
    }
    

    How do I actually connect the QLineEdit object called "edit" to the QTableWidget object?

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    mrjjM 1 Reply Last reply
    0
    • GuerrianG Guerrian

      @SGaist said in How to apply validation on cells in a QTableWidget?:

      QLineEdit

      For 1 here is what I have so far:

      void MainWindow::addSub(int row, int col)
      {
          auto *edit = new QLineEdit;
          QRegExp regExp("/^[^'\"]*$/");
          auto *validator = new QRegExpValidator(regExp, this);
          edit->setValidator(validator);
          auto item = new QTableWidgetItem();
          ui->tableWidget->setItem(row, col, item);
      }
      
      void MainWindow::addRow(int &last)
      {
          last = ui->tableWidget->rowCount();
          ui->tableWidget->insertRow(last);
          addSub(last, 0);
      }
      

      How do I actually connect the QLineEdit object called "edit" to the QTableWidget object?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #9

      @Guerrian
      Hi
      you cannot connect external QLineEdit.
      You must do as in example and make delegate.
      http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html
      However, instead of spin box, you returns you LineEdit with validator ( as code is now)
      in CreateEditor.
      Its important to read about how delegates works.
      http://doc.qt.io/qt-5/qstyleditemdelegate.html

      GuerrianG 1 Reply Last reply
      1
      • mrjjM mrjj

        @Guerrian
        Hi
        you cannot connect external QLineEdit.
        You must do as in example and make delegate.
        http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html
        However, instead of spin box, you returns you LineEdit with validator ( as code is now)
        in CreateEditor.
        Its important to read about how delegates works.
        http://doc.qt.io/qt-5/qstyleditemdelegate.html

        GuerrianG Offline
        GuerrianG Offline
        Guerrian
        wrote on last edited by
        #10

        @mrjj Do I have to use a model / view based architecture (QTableView) to go down this path?

        Linux Mint 18.3
        Qt 5.14.1
        Qt Creator 4.11.1

        mrjjM 1 Reply Last reply
        0
        • GuerrianG Guerrian

          @mrjj Do I have to use a model / view based architecture (QTableView) to go down this path?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #11

          @Guerrian
          no, it works the same via setItemDelegateForColumn and friends.
          The *Widgets version are also views, but comes wrapped up with own internal model and
          its meant for convenience.
          However, in the long run, you might be more happy with *Views.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #12

            To add to @mrjj, use QRegularExpression and not QRegExp, that one is deprecated.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • GuerrianG Offline
              GuerrianG Offline
              Guerrian
              wrote on last edited by VRonin
              #13

              When I add the delegate to the QTableWidget the cells disappear i.e. there is no text editor in the table. I assume this is because I have not implemented a paint method for the delegate. I do no know how to do this:

              class TextDelegate : public QStyledItemDelegate
              {
                  Q_OBJECT
              public:
                  TextDelegate(QObject *parent = 0);
                  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override;
                  void setEditorData(QWidget *editor, const QModelIndex &index) const override;
                  void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
                  void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
              };
              
              TextDelegate::TextDelegate(QObject *parent): QStyledItemDelegate(parent)
              {
              }
              
              QWidget *TextDelegate::createEditor(QWidget *parent,
                  const QStyleOptionViewItem &,
                  const QModelIndex &) const
              {
                  QLineEdit *editor = new QLineEdit(parent);
                  editor->setFrame(false);
                  QRegularExpression regExp("/^[^'\"]*$/");
                  auto *validator = new QRegularExpressionValidator(regExp, parent);
                  editor->setValidator(validator);
                  return editor;
              }
              
              void TextDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
              {
                  QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
                  lineEdit->setText("test value");
              }
              
              void TextDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
              {
              }
              
              void TextDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
              {
                  editor->setGeometry(option.rect);
              }
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  ui->tableWidget->setHorizontalHeaderLabels(QStringList() << tr("Name"));
                  TextDelegate delegate;
                  ui->tableWidget->setItemDelegate(&delegate);
              }
              

              Linux Mint 18.3
              Qt 5.14.1
              Qt Creator 4.11.1

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #14

                Hi
                Do you mean when not in edit mode ?

                GuerrianG 1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  Do you mean when not in edit mode ?

                  GuerrianG Offline
                  GuerrianG Offline
                  Guerrian
                  wrote on last edited by Guerrian
                  #15

                  @mrjj I mean the cells have disappeared from the screen in all modes. There is nothing in the table except for the column and row text.

                  0_1526680347205_Qt Table.jpg

                  Linux Mint 18.3
                  Qt 5.14.1
                  Qt Creator 4.11.1

                  mrjjM 1 Reply Last reply
                  0
                  • GuerrianG Guerrian

                    @mrjj I mean the cells have disappeared from the screen in all modes. There is nothing in the table except for the column and row text.

                    0_1526680347205_Qt Table.jpg

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by VRonin
                    #16

                    @Guerrian
                    Hi
                    There is a natural explanation to that.
                    You delegate runs out of scope and gets deleted. :)

                    TextDelegate delegate; // local var, wont survive the constructor
                    ui->tableWidget->setItemDelegate(&delegate);
                    

                    do like this
                    ui->tableWidget->setItemDelegate(new TextDelegate(this));

                    and it will draw as expected.

                    GuerrianG 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @Guerrian
                      Hi
                      There is a natural explanation to that.
                      You delegate runs out of scope and gets deleted. :)

                      TextDelegate delegate; // local var, wont survive the constructor
                      ui->tableWidget->setItemDelegate(&delegate);
                      

                      do like this
                      ui->tableWidget->setItemDelegate(new TextDelegate(this));

                      and it will draw as expected.

                      GuerrianG Offline
                      GuerrianG Offline
                      Guerrian
                      wrote on last edited by VRonin
                      #17

                      @mrjj Nice one. I got further with this, but two problems still remain:

                      1. The editor still allows quotes in the input.
                      2. The text in the QTableWidget is not being updated when I leave the cell.

                      In the case of 2 I added a debug line, but the function doesn't get called:

                      void TextDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                      {
                          QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
                          QString value = lineEdit->text();
                          qDebug() << "value: " << value;
                          model->setData(index, value, Qt::EditRole);
                      }
                      

                      This is what my createEditor function looks like now:

                      QWidget *TextDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
                      {
                          QLineEdit *lineEdit = new QLineEdit(parent);
                          lineEdit->setFrame(false);
                          QRegularExpression regExp("/^[^'\"]*$/");
                          QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
                          lineEdit->setValidator(validator);
                          QSignalMapper *mapper = new QSignalMapper(lineEdit);
                          mapper->setMapping(lineEdit, 0);
                          QObject::connect(lineEdit, SIGNAL(textEdited(bool)), mapper, SLOT(map()));
                          QObject::connect(mapper, SIGNAL(mapped(QWidget *)), this, SIGNAL(commitData(QWidget *)));
                          return lineEdit;
                      }
                      

                      I'm not sure about the id parameter in the mapping.

                      Linux Mint 18.3
                      Qt 5.14.1
                      Qt Creator 4.11.1

                      VRoninV 1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #18

                        What are you doing with that QSignalMapper ?

                        By the way, use qobject_cast and not static_case with QObject based classes.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        GuerrianG 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          What are you doing with that QSignalMapper ?

                          By the way, use qobject_cast and not static_case with QObject based classes.

                          GuerrianG Offline
                          GuerrianG Offline
                          Guerrian
                          wrote on last edited by
                          #19

                          @SGaist said in How to apply validation on cells in a QTableWidget?:

                          What are you doing with that QSignalMapper ?

                          I'm not sure, this is all new to me.

                          By the way, use qobject_cast and not static_case with QObject based classes.

                          Thanks

                          Linux Mint 18.3
                          Qt 5.14.1
                          Qt Creator 4.11.1

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #20

                            In that case, don't use it. Follow the SpinBox Delegate example as suggested and just replace the QSpinBox editor by the QLineEdit + QRegularExpressionValidator. And IIRC, you even just have to re-implement only the createEditor method

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            GuerrianG 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              In that case, don't use it. Follow the SpinBox Delegate example as suggested and just replace the QSpinBox editor by the QLineEdit + QRegularExpressionValidator. And IIRC, you even just have to re-implement only the createEditor method

                              GuerrianG Offline
                              GuerrianG Offline
                              Guerrian
                              wrote on last edited by Guerrian
                              #21

                              @SGaist
                              OK, but why is my setModelData not being executed?

                              Linux Mint 18.3
                              Qt 5.14.1
                              Qt Creator 4.11.1

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #22

                                Did you remove the mapper ?
                                Are you sure that your editor is used in the first place ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                GuerrianG 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  Did you remove the mapper ?
                                  Are you sure that your editor is used in the first place ?

                                  GuerrianG Offline
                                  GuerrianG Offline
                                  Guerrian
                                  wrote on last edited by
                                  #23

                                  @SGaist said in How to apply validation on cells in a QTableWidget?:

                                  Did you remove the mapper ?

                                  Yes.

                                  Are you sure that your editor is used in the first place ?

                                  Well yes, because createEditor is called when I click on the QTableWidget to edit a cell.

                                  Linux Mint 18.3
                                  Qt 5.14.1
                                  Qt Creator 4.11.1

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #24

                                    Can you show your current call and a minimal QTableWidget setup that uses it ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    1
                                    • GuerrianG Guerrian

                                      @mrjj Nice one. I got further with this, but two problems still remain:

                                      1. The editor still allows quotes in the input.
                                      2. The text in the QTableWidget is not being updated when I leave the cell.

                                      In the case of 2 I added a debug line, but the function doesn't get called:

                                      void TextDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                                      {
                                          QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
                                          QString value = lineEdit->text();
                                          qDebug() << "value: " << value;
                                          model->setData(index, value, Qt::EditRole);
                                      }
                                      

                                      This is what my createEditor function looks like now:

                                      QWidget *TextDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
                                      {
                                          QLineEdit *lineEdit = new QLineEdit(parent);
                                          lineEdit->setFrame(false);
                                          QRegularExpression regExp("/^[^'\"]*$/");
                                          QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
                                          lineEdit->setValidator(validator);
                                          QSignalMapper *mapper = new QSignalMapper(lineEdit);
                                          mapper->setMapping(lineEdit, 0);
                                          QObject::connect(lineEdit, SIGNAL(textEdited(bool)), mapper, SLOT(map()));
                                          QObject::connect(mapper, SIGNAL(mapped(QWidget *)), this, SIGNAL(commitData(QWidget *)));
                                          return lineEdit;
                                      }
                                      

                                      I'm not sure about the id parameter in the mapping.

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #25

                                      @Guerrian said in How to apply validation on cells in a QTableWidget?:

                                      The editor still allows quotes in the input.

                                      That's because your regular expression is wrongly escaped, raw string literals are a blessing in cases like this one: QRegularExpression regExp(QStringLiteral(R"**(^[^'\"]*$)**"));

                                      The text in the QTableWidget is not being updated when I leave the cell.

                                      Make sure you use override in the declaration of setModelData to avoid the usual pitfall.

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      GuerrianG 1 Reply Last reply
                                      2
                                      • VRoninV VRonin

                                        @Guerrian said in How to apply validation on cells in a QTableWidget?:

                                        The editor still allows quotes in the input.

                                        That's because your regular expression is wrongly escaped, raw string literals are a blessing in cases like this one: QRegularExpression regExp(QStringLiteral(R"**(^[^'\"]*$)**"));

                                        The text in the QTableWidget is not being updated when I leave the cell.

                                        Make sure you use override in the declaration of setModelData to avoid the usual pitfall.

                                        GuerrianG Offline
                                        GuerrianG Offline
                                        Guerrian
                                        wrote on last edited by
                                        #26

                                        @VRonin
                                        After I corrected the regular expression, my code worked.

                                        Linux Mint 18.3
                                        Qt 5.14.1
                                        Qt Creator 4.11.1

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved