SectionScroller vs. QAbstractProxyModel

In this post I wrote about how to use a Qt model with the QML SectionScroller. A more advanced use case is to encapsulate such a “basic” model in a QAbstractProxyModel instance like QSortFilterProxyModel.

This at first will brake your QML SectionScroller as some important methods and properties are missing in the proxy that are present in your initial model. However, there is no need for being in despair; adding the missing bits is quite straight forward and simply requires to forward the missing method calls to your initial model.

Recall that we added (amongst others) a read only “count” property, its corresponding accessor method, and a “get(int index)” method that returns the item in the model at the given index to our initial model to make SectionScroller work. By encapsulating the initial model in a proxy those parts are not accessible for the SectionScroller code anymore. What we need to do to fix this issue is to simply add some forwarding methods plus the missing property and we are back in business. I took the following code excerpts from the current MeePasswords version as can be found on Gitorious:

...
class EntrySortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
// Needed to make SectionScroller happy.
Q_PROPERTY(int count READ rowCount)

public:
...
// Needed to make SectionScroller happy.
Q_INVOKABLE Entry* get(int index) { return ((EntryListModel*)sourceModel())->get(index); }
Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const {
return ((EntryListModel*)sourceModel())->rowCount(parent);
}
    ...
};
...
Advertisement
This entry was posted in Qt/QML, Snippets and tagged , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.