Congratulations to our 2022 Qt Champions!

QTableView QValidator.

  • Hi,

    I am displaying some editable data with a QTableView.
    I want to validate some data with a QValidator. This should be possible.
    but i cant find the needed functions.

    @Qtableview tableView;
    tableView.setmodel( ... );

    QIntValidator* intValidator(0,11,tableView);

    // tableView.setvalidator(column 2, validator intValidator);@

    Any help is appreciated, thanks.

  • Moderators

    what exactly do you want to do?
    Validate the existing data of a table or the data the user can edit in the table?

  • Data input from the user.

  • Lifetime Qt Champion

    Then you'll have to make a "custom item delegate": setting up the validator when creating the editor

  • which function creates the editor?
    @QWidget * QStyledItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const@

    But how will i set the validator to the widget?

    @class tableValidator : public QStyledItemDelegate
    explicit tableValidator(QValidator* validator=0, QObject *parent = 0) : QStyledItemDelegate(parent)
    Validator = validator;

    virtual ~tableValidator()


    public slots:

    QValidator* Validator;


  • Moderators

    easiest way would be probably this:
    QWidget * MyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    QWidget* editor = QStyledItemDelegate::createEditor(parent, option, index);
    QLineEdit* lineEditEditor = qobject_cast<QLineEdit*>(editor);
    if( lineEditEditor )

     return editor;


  • Thanx guys.

Log in to reply