[Qt-qml] QSortFilterProxyModel with QML ListView

Stephen Kelly steveire at gmail.com
Tue Sep 14 17:48:19 CEST 2010


Cornelius Hald wrote:

> Hi,
> 
> I've now managed to use my own data model together with a customized
> QSortFilterProxyModel to provide sorting and searching for my QML UI. I
> still have two open questions, though.
> 
> 1) Is there a way to animate sorting? I have animations for inserting
> and removing data and they are also used when filtering (very nice). But
> I would also like to give the user some feedback about changed sorting
> as well.

Not in the current design of the QML model handling stuff because 
QPersistentModelIndexes are not used by QML.

How do you do animate the items in and out? Undocumented internals or some 
other way?

> 
> 2) In my proxy class I have to call beginResetModel() and
> endResetModel() around my sorting code. Otherwise the QML ListView does
> not update itself. Is this really needed? I thought the
> QSortFilterProxyModel class would take care about that itself. Making
> the filter work doesn't need these calls.
> 
> My code looks like this:
> 
> SortedModel::SortedModel(QObject *parent) :
>     QSortFilterProxyModel(parent)
> {
> }
> 
> void SortedModel::sortBy(QString roleName)
> {
>     beginResetModel(); // Do I really need that?
>     if (roleName == "name") {
>         setSortRole(MyModel::NameRole);
>     } else if (roleName == "description") {
>         setSortRole(MyModel::DescriptionRole);
>     }
>     endResetModel();
> }
> 
> void SortedModel::search(QString search)
> {
>     setFilterCaseSensitivity(Qt::CaseInsensitive);
>     setFilterRole(MyModel::NameRole);
>     setFilterWildcard(search);
> }

Fixed in Qt 4.7.1.

http://bugreports.qt.nokia.com/browse/QTBUG-13146

All the best,

Steve

> 
> Thanks (again) :)
> Conny



More information about the Qt-qml mailing list