Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Set validator to custom QAbstractItemModel (tree model)



  • Hi,

    I have custom items and custom tree model inherited from QAbstractItemModel that nested to proxy model inherited fromQSortProxyModel :)
    Now I would like to set a validator that checks user input on item's name. I expect the same behaviour as QLineEdit has with validator set. So I'm trying to avoid cheks in bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) like if (role == Qt::EditRole) beacause it works only after user accept input (press Enter or something)

    How can I achieve that?



  • This is a task for the delegate, not the model. Reimplement QStyledItemDelegate and make it crate a QLineEdit with a custom validator



  • This is a task for the delegate, not the model. Reimplement QStyledItemDelegate and make it crate a QLineEdit with a custom validator



  • @VRonin thank you!
    I've found an example here

    I've spent one day to understand why above example didn't work for me (QIntValidator didn't restrict my input even if I write letter). So here is some information for those who encounter the same problem

    I noticed one thing. Let's suppose that we have QLineEdit with set QIntValidator and this line by default has some statement like abc. When we try to add any symbol/character to this line then QIntValidator doesn't block our input and we are allowed to write it. To make Validator work we need firstly remove all symbols that are not allowed by the validator and only then validator starts to work (we will be able to write only integer).


Log in to reply