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



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


  • Lifetime Qt Champion

    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);


  • Lifetime Qt Champion

    Hi,

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



  • 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


  • Lifetime Qt Champion

    @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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.