UI doesn't update when Python QAbstractListModel changes from a Thread

  • I posted the following as a QT bug, but after further work I am beginning to think that I am missing something:

    When running the attached code the UI doesn't update until i resize the window, and I get the following error:

    QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
    (Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)

    I am not sure if this is PySide 2 bug, an underlying QT issue, or I am supposed to do something in my code to make this work.

    Also I reported the issue as 5.12.2 of QT but it may be in 5.11.2, as I have both installed.

    So I am now wondering if the issue is that i somehow have to update the QAbstractListModel from the UI thread and not from my own thread -- the equivalent of Platform.runLater in JavaFX. So how do I do that?

    Also how do I actually change the ListView's model from Python? I don't want to change the contents of the ListView's model, I want to change it to a completely different model ....


    -- here is the code -- can't upload it for some reason

    -- python code
    import sys
    from PySide2.QtCore import QAbstractListModel, Qt, QByteArray, SIGNAL, QModelIndex
    from PySide2.QtGui import QGuiApplication
    from PySide2.QtWidgets import QListView
    from PySide2.QtQuick import QQuickView
    from PySide2.QtQml import QQmlComponent
    from PySide2.QtQml import qmlRegisterType
    class PersonModel (QAbstractListModel):
        MyRole = Qt.UserRole + 1
        def __init__(self, parent = None):
            QAbstractListModel.__init__(self, parent)
            self._data = []
        def roleNames(self):
            roles = {
                PersonModel.MyRole : QByteArray(b'modelData'),
                Qt.DisplayRole : QByteArray(b'display')
            return roles
        def rowCount(self, parent=QModelIndex()):
            return len(self._data)
        def data(self, index, role):
            d = self._data[index.row()]
            if role == Qt.DisplayRole:
                return d['name']
            elif role == Qt.DecorationRole:
                return Qt.black
            elif role == PersonModel.MyRole:
                return d['myrole']
            return None
        def appendRow(self, n, t):
            self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
            self._data.append({'name':n, 'myrole':t})
        def populate(self):
            self.appendRow('Asterix', 'role1')
            self.appendRow('Obelix', 'role2')
    from threading import Thread
    from time import sleep
    def function01(arg, myModel, name):
        for i in range(arg):
            print (name,"arg---->",arg,'\n')
            myModel.appendRow(name + str(i),  'role1')
            # index = len(myModel._data)
            # myModel.foo(index)
    if __name__ == '__main__':
        app = QGuiApplication(sys.argv)
        # qmlRegisterType(PersonModel, 'Charts', 1, 0, 'PersonModel');
        # qmlRegisterType(PersonModel, 'Charts', 1, 0, 'PersonModel');
        view = QQuickView()
        myModel = PersonModel()
        view.rootContext().setContextProperty("myModel", myModel)
        thread1 = Thread(target=function01, args=(5, myModel, 'thread1',))
        # thread1.join()
        # thread2.join()
        # Deleting the view before it goes out of scope is required to make sure all child QML instances
        # are destroyed in the correct order.
        del view
    -- view.qml
    import QtQuick 2.0
    ListView {
        id : xxx
        width: 100
        height: 100
        anchors.fill: parent
        model: myModel
        delegate: Component {
            Rectangle {
                height: 25
                width: 100
                Text {
                    function displayText() {
                        var result = ""
                        if (typeof display !== "undefined")
                            result = display + ": "
                        result += modelData
                        return result
                    text: displayText()

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.