[Interest] Sorting QSortFilterProxyModel with QML

Shantanu Tushar shaan7in at gmail.com
Fri May 22 19:05:38 CEST 2015


I use a QSortFilterProxyModel with a TableView and using
http://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#sortIndicatorColumn-prop
like this-

TableView {
...
...
sortIndicatorVisible: true
onSortIndicatorColumnChanged:
model.sort(getColumn(sortIndicatorColumn).role, sortIndicatorOrder)
...
...
}

and the sort method is defined like this-

void ModelFilter::sort(const QString &role, Qt::SortOrder order)
{
    setSortRole(sourceModel()->roleNames().key(role.toLatin1()));

    QSortFilterProxyModel::sort(0, order);
}

This works pretty well for me.

On Fri, May 22, 2015 at 9:46 PM, Mathias Baumann <qt at supradigital.org>
wrote:

> 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
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>



-- 
Shantanu Tushar    (UTC +0530)
shantanu.io
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150522/5071af93/attachment.html>


More information about the Interest mailing list