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 set the maximum length of characters in the cellWidget (QTableWidget) by typing from the keyboard
Forum Updated to NodeBB v4.3 + New Features

how to set the maximum length of characters in the cellWidget (QTableWidget) by typing from the keyboard

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.7k 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.
  • D Offline
    D Offline
    Domenico
    wrote on last edited by
    #1

    Hi everyone,

    I would like some help .. how can I set the maximum length of characters in a specific cellWidget (QTableWidget) that is in a certain column and in a certain line by typing from the keyboard?

    I know that with QLineEdit::setMaxLenght (10) it is possible to type up to 10 characters from the keyboard, whereas on QTableWidget with which function is used? you can do with an example of codes ..

    thanks a lot !!

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

      Hi
      You could use a Delegate where you create your own lineEdit and limit its text.

      class LimitTextDelegate : public QStyledItemDelegate
      {
          Q_OBJECT
      
      public:
          LimitTextDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
      
          QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                const QModelIndex &index) const
          {
              QLineEdit *editor = new QLineEdit(parent);
              int maxChar = index.row() + 1 ; // change this to suit you
              editor->setMaxLength(maxChar);
              return editor;
          }
          void setEditorData(QWidget *editor, const QModelIndex &index) const
          {
              QString value = index.model()->data(index, Qt::EditRole).toString();
              QLineEdit *line = qobject_cast<QLineEdit *>(editor);
              if (line) line->setText(value);
          }
          void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
          {
              QLineEdit *line = qobject_cast<QLineEdit *>(editor);
              if (line) {
                  QString value = line->text();
                  model->setData(index, value);
              }
          }
          void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
          {
              editor->setGeometry(option.rect);
          }
      };
      
      

      just set it with
      ui->tableWidget->setItemDelegate(new LimitTextDelegate);

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

        Hi,

        @mrjj suggestion is good. However, it can be simplified. You just need the createEditor function.

        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
        3
        • D Offline
          D Offline
          Domenico
          wrote on last edited by
          #4

          thank you so much! it worked.

          I would need another help. when I type from the keyboard all the letters are entered within the max length of characters I choose (setMaxLenght (4)) ...

          but I wish that only the numbers "0" or "1" are entered, not all the letters from the keyboard ..

          how can i implement in code?

          Thanks for your help

          mrjjM 1 Reply Last reply
          0
          • D Domenico

            thank you so much! it worked.

            I would need another help. when I type from the keyboard all the letters are entered within the max length of characters I choose (setMaxLenght (4)) ...

            but I wish that only the numbers "0" or "1" are entered, not all the letters from the keyboard ..

            how can i implement in code?

            Thanks for your help

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

            @Domenico
            Hi
            You can try setting a mask on it
            https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop
            the B flag.
            else we can override keypress and only accept 0 and 1

            1 Reply Last reply
            3
            • D Offline
              D Offline
              Domenico
              wrote on last edited by
              #6

              I found a solution. thank you very much for helping!

              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