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 asQLineEdit
has with validator set. So I'm trying to avoid cheks inbool MyModel::setData(const QModelIndex &index, const QVariant &value, int role)
likeif (role == Qt::EditRole)
beacause it works only after user accept input (pressEnter
or something)How can I achieve that?
-
@VRonin thank you!
I've found an example hereI'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 problemI noticed one thing. Let's suppose that we have
QLineEdit
with setQIntValidator
and this line by default has some statement likeabc
. When we try to add any symbol/character to this line thenQIntValidator
doesn't block our input and we are allowed to write it. To makeValidator
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).