[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 ? -
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 .? -
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 -
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).