[Interest] Question about advice regarding dynamicSortFilter of QSFPM in the docs

Elvis Stansvik elvstone at gmail.com
Wed Jun 1 09:33:16 CEST 2016


2016-06-01 9:02 GMT+02:00 Elvis Stansvik <elvstone at gmail.com>:
> 2016-06-01 8:54 GMT+02:00 Elvis Stansvik <elvstone at gmail.com>:
>> Hi all,
>>
>> I've used QSortFilterProxyModel in the past, but it was quite a while
>> ago and now I'm a little confused by the advice given in the docs.
>>
>> The description of the dynamicSortFilter property [1] reads:
>>
>> "Note that you should not update the source model through the proxy
>> model when dynamicSortFilter is true. For instance, if you set the
>> proxy model on a QComboBox, then using functions that update the
>> model, e.g., addItem(), will not work as expected. An alternative is
>> to set dynamicSortFilter to false and call sort() after adding items
>> to the QComboBox.
>>
>> The default value is true."
>>
>> I can't remember this restriction from when I've worked with it
>> before, and in the class description, underneath the second code
>> snippet, it says:
>>
>> "Any changes made through the QSortFilterProxyModel are applied to the
>> original model."
>>
>> As if it is safe to update the model through the QSFPM at this point,
>> even if it is speaking of one where dynamicSortFilter is true (the
>> default).
>>
>> Surely it must be possible to use a QSFPM with dynamicSortFilter set
>> to true as the model of some view which also provides editing (and
>> consequently will make its changes through the QSFPM)? Or is that
>> simply not a supported use case? If I use dynamicSortFilter, must I
>> always make my changes to the source model?
>
> As a simple concrete example, say I create a QStringListModel, wrap it
> in a QSFPM (leaving dynamicSortFilter enabled) and set that QSFPM as
> the model of a QListView with editing enabled.
>
> The docs make it sound like that's inadvisable (since the QListView
> will perform its editing through the dynamically sorted QSFPM). Is
> that right?

I made this basic test, which didn't show any ill effects when I made
an edit through the QSFPM:


from sys import argv, exit

from PyQt5.QtCore import QCoreApplication, QTimer, Qt
from PyQt5.QtCore import QSortFilterProxyModel, QStringListModel

app = QCoreApplication(argv)

def printRows(model):
    for row in range(model.rowCount()):
        print(model.data(model.index(row, 0), Qt.DisplayRole))

def test():
    sourceModel = QStringListModel()
    sourceModel.setStringList(['B', 'A', 'C'])

    # QSFPM with dynamicSortFilter enabled
    proxyModel = QSortFilterProxyModel()
    proxyModel.setSourceModel(sourceModel)
    proxyModel.sort(0)

    print('sourceModel:')
    printRows(sourceModel)

    print('proxyModel before edit:')
    printRows(proxyModel)

    # Change the 'B' to an 'E' through the QSFPM
    proxyModel.setData(proxyModel.index(1, 0), 'E')

    print('proxyModel after edit:')
    printRows(proxyModel)

    app.quit()

QTimer.singleShot(0, test)

exit(app.exec_())


Result:


[estan at pyret ~]$ python test.py
sourceModel:
B
A
C
proxyModel before edit:
A
B
C
proxyModel after edit:
A
C
E
[estan at pyret ~]$


So either I'm misreading the docs, or my example is too simple.

Any ideas?

Elvis

>
> Elvis
>
>>
>> Grateful for any advice,
>> Elvis
>>
>> [1] http://doc.qt.io/qt-5/qsortfilterproxymodel.html#dynamicSortFilter-prop
>> [2] http://doc.qt.io/qt-5/qsortfilterproxymodel.html#details



More information about the Interest mailing list