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. Set validator to custom QAbstractItemModel (tree model)

Set validator to custom QAbstractItemModel (tree model)

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 544 Views
  • 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.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Please_Help_me_DP 1 Reply Last reply
      2
      • VRoninV VRonin

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

        Please_Help_me_DP Offline
        Please_Help_me_DP Offline
        Please_Help_me_D
        wrote on last edited by
        #3

        @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).

        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