[Interest] Sorting QSortFilterProxyModel with QML

Mathias Baumann qt at supradigital.org
Fri May 22 18:16:05 CEST 2015


Hello,

I am using a QSortFilterProxyModel with QML. I with it from my c++ code
by doing for example:

    sf_server_model.setSourceModel(&server_model);
    sf_server_model.setDynamicSortFilter(true);
    sf_server_model.setSortRole(ServerModel::Ping);
    sf_server_model.sort(0);

However, it becomes more difficult if I want to do sorting from QML,
specifically from a TableView. 

I am using the headerDelegate property to have my own table header.

I thought I could have some kind of MouseArea in there that reacts on
click and calls some Q_INVOKABLE function on the model that I
previously defined giving it the role or role-id as parameter.

But in the headerDelegate I have no model.role available. The only
thing I really have in there that would help me to identify which
role/column I am currently in is styleData.value which is the literal
string that appears in the header, thus subject to localization (thus
not exactly ones first choice to use as check in your code). 

I do have the column number to (styleData.column) too, but that column
is completely separate from the thing that QSortFilterProxyModel
understands as column (if I understood that right). 

The styleData.column seems to change based on how I arrange my columns
in QML, so I am rather sure it's separate, thus also no big help.

I could hard-code the column numbers I use in the QML and manual map
them to the actual role, but that seems rather dirty and is likely to
break with every change to the column order or addition of columns.

So my question is: What is the best way to get sorting of a TableView
using a QSortFilterProxyModel when clicking on the according column
headers?

cheers

    --Marenz



More information about the Interest mailing list