Dynamically select TreeView item.

  • I have a signal/slot hooked up to return an object that exists within my treeview model.

    My question is, how can I find the particular item in the tree view based on this model data? Do I need to traverse the entire tree structure to find a match?

    Does something like below exist?

    QModelIndex indexOfTreeItem = m_pCurrentModel->findIndex(modelData);


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you using a custom model ? If so, it's up to you to provide the findIndex method for your model.

  • Moderators

    probably the best would be to store the object in a hash-table for quick lookup.
    But make sure to cleanup the hash table once the object is destroyed to avoid dumping the memory.

  • @SGaist Thanks. I'm not using a custom model, though it's looking like I might need to write a custom method to find the value.

  • Moderators

    about how many tree items are we talking here?
    Is the model static or dynamic (items added/removed during runtime)?

Log in to reply

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