Qt World Summit: Register Today!

QSortProxyModel dynamicSortFilter Problem

  • Just wanted to check this before posting a QT bug on the same.

    I have a CustomListModel which is the sourceModel for a QSortFilterProxyModel.

    Now with dynamic sorting and filtering set, everything is fine if the first item added in the sourceModel is accepted by the filter on the ProxyModel.

    However when i had all items in the sourceModel as invalid in the ProxyModel and then i change an item in the sourceModel and emit dataChanged which called filterAcceptsRow and that returned true, I have one item visible in the view holding the ProxyModel.
    Now the lessThan function (for Sort) never got called.

    I did some digging through the qsortfilterproxymodel.cpp and found source_sort_column as -1.

    void QSortFilterProxyModelPrivate::sort_source_rows(
    QVector<int> &source_rows, const QModelIndex &source_parent) const
    Q_Q(const QSortFilterProxyModel);
    if (source_sort_column >= 0) {
    if (sort_order == Qt::AscendingOrder) {
    QSortFilterProxyModelLessThan lt(source_sort_column, source_parent, model, q);
    qStableSort(source_rows.begin(), source_rows.end(), lt);
    } else {
    QSortFilterProxyModelGreaterThan gt(source_sort_column, source_parent, model, q);
    qStableSort(source_rows.begin(), source_rows.end(), gt);

    Any further updates into the sourceModel has expected behaviour in the Proxy with filter however lessThan never gets called because of the above function.

    Just to confirm if this was the problem as soon as the first dataChanged fired i called the QSortFilterProxyModel->invalidate() and everything was fine.

    It seems like the source_sort_column does not get updated like how it updates via RowInserted or removed when coming from dataChanged and filterAccepts returns true.

    Am i totally missing something obvious here or does it sound wrong to you guys as well?

  • Did you call proxy->setDynamicSortFilter(true); ?

  • yep

    and in this order

    live_contacts_->sort(0, Qt::AscendingOrder);

    Also once the dataChanged signal is emitted if i call invalidate() on the proxy model it behaves as expected after that.

Log in to reply