[solved] to get QStandardItemModel item by key without looping using rowCount()?
-
wrote on 3 Aug 2011, 13:04 last edited by
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 ? -
wrote on 3 Aug 2011, 13:40 last edited by
QStandardItemModel::findItems()
-
wrote on 3 Aug 2011, 14:36 last edited by
Thanks ! that helped
-
wrote on 4 Aug 2011, 06:40 last edited by
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 .? -
wrote on 4 Aug 2011, 07:11 last edited by
Eddy : merged the 2 topics because basically they are about the same issue.
-
wrote on 4 Aug 2011, 07:40 last edited by
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);
QList<QStandardItem> items;
for (int i = 0; i < indexes.size(); ++i)
items.append(model->itemFromIndex(indexes.at(i)));
return items;@
also have a look at the appropriate "flags":http://doc.qt.nokia.com/4.7/qt.html#MatchFlag-enum -
wrote on 4 Aug 2011, 07:45 last edited by
what do you think will be faster ? to stay with the model rowCount or go with like your suggestion ?
-
wrote on 4 Aug 2011, 07:51 last edited by
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).
-
wrote on 4 Aug 2011, 08:05 last edited by
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 -
wrote on 4 Aug 2011, 08:15 last edited by
QModelIndex has the sibling() function. I guess that would be the best option, because it's aware of the treeview hierarchy.
@
QModelIndex index;
index.sibling(index.row(),column);
@ -
wrote on 4 Aug 2011, 08:40 last edited by
Thanks , i guess to get the row index number i can get with item->row() . right?
-
wrote on 4 Aug 2011, 09:03 last edited by
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
-
wrote on 4 Aug 2011, 10:26 last edited by
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] ?Thanks
5/13