How to check if an item exist in a model?
-
Hi,
How to do that for a QSqlTableModel and/or QSortFilterProxyModel. -
[quote author="broadpeak" date="1323703488"]Maybe helps:
use a temporary model, and set this filter exactly:
[/quote]It depends on the number of items in the model (and on their type), having another clone model is not an efficient solution for huge amount of data. Another solution that seems more efficient to me is to keep an hash table with the column data as key and the model index as value. This could be simpler for huge amount of data and exact match searching for.
But iterating would be my first choice. -
Thanks, Trying now...
-
[quote author="Andre" date="1323702362"]unless you know that the data will be sorted. [/quote]
Yes, in my case I have a sorted QSqlSortFilterModel, so what I need to do?
-
(Ther is no "QSqlSortFilterModel", but we have QSortFilterModel, because this can filter and sort every model, not only sql_model)
So with FilterModel you can try it:
@
proxyModel->setFilterRegExp(QRegExp("*.txt", Qt::CaseInsensitive, QRegExp::FixedString));
@(I don't know how to iterate through on a filtered model :S)
-
Well, if you know the model is sorted, you can use a binary search algorithm. You basically first try the item in the middle of the list, if that item is bigger than the one you were looking for, you can try the one half way the beginning and the item you just tried, otherwise you try the one between the middle and the end. That way, you can home in on the place in the list where your item is or should be very quickly. It is much faster than just iterating over all items (assuming random access costs are small, of course; if random access is much more expensive than sequential access, a linear search untill you are at an item bigger than what you were looking for might still be cheaper).
Edit
Slower, but less code, is to just create a new QSortFilterProxyModel, set the proxy model you already have a the source model, set the filter to whatever you are searching for. If the model contains more than 0 items, you have found your item. However, that is not as efficient as searching yourself, I think. -
Am I missing something, or would "QAbstractItemModel::match() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#match be of use here?
-
[quote author="Volker" date="1323734916"]Am I missing something, or would "QAbstractItemModel::match() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#match be of use here?[/quote]
You are missing nothing, I am. This is of course much easier than anything I suggested before.
-
Thanks for you all. I try..