Important: Please read the Qt Code of Conduct -

[solved] to get QStandardItemModel item by key without looping using rowCount()?

  • im using QStandardItemModel as my tree view model , im getting some string as input
    the input value can be or not in one of the QStandardItemModel items .
    right now im using looping all the rows to find out the value and then manipulate the item.
    can i just get the item by the input as key ( like hash map or something ) to save me the loop ?

  • QStandardItemModel::findItems()

  • Thanks ! that helped

  • I have Treeview as view , and QStandardItemModel as the model also using QSortFilterProxyModel subclass
    to implement less ten method for sorting .
    Now I have input that automatically updates the treeview . the input comes as structure that contains id's and there data .
    ( each id has 2 or 3 columns of data that needs to be represents as colums in the row ).
    Now . what im doing now is looping thorw all model with rowCount. This could be long loop .
    In this loop im checking each first column Qt::UserRule Data of each row becose this where I store the id.
    Now my question is . how can do something like findItems that finds in UserRule data .?

  • Eddy : merged the 2 topics because basically they are about the same issue.

  • Something like this might work:
    QModelIndexList indexes = model->match(model->index(0, 0 /* column /, QModelIndex()),
    Qt::UserRole, id /
    your data /, -1 / find all matches /, flags);
    > items;
    for (int i = 0; i < indexes.size(); ++i)
    return items;

    also have a look at the appropriate "flags":

  • what do you think will be faster ? to stay with the model rowCount or go with like your suggestion ?

  • I don't think there will be much of a difference. However you could try to benchmark the two (use an an average data set for your application, start a timer, and stop it after you used one function 10000 times and then you do the same thing with the other method).

  • Hi
    its working great anther question if i can , how can i get / navigate all the column that in the same row
    as the item i found ? basically i need to get all items from this row .
    Thanks allot

  • QModelIndex has the sibling() function. I guess that would be the best option, because it's aware of the treeview hierarchy.

    QModelIndex index;

  • Thanks , i guess to get the row index number i can get with item->row() . right?

  • Can i ask last question on this thread .
    if i have item and this item has child nods , how can i remove all the child nods under the selected
    item ?

    Thanks Allot

  • Hi Umen,

    I see that you started a new topic with your last question.
    If you consider this thread solved can you edit the title and add [solved] ?


Log in to reply