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.
  • 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