Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. UI doesn't update when Python QAbstractListModel changes from a Thread
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2qt for python
1 Posts 1 Posters 668 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qtpublicname
    wrote on last edited by qtpublicname
    #1

    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 ....

    thanks
    G

    -- 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})
            self.endInsertRows()
    
        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,'i---->',i,'\n')
            print (name,"arg---->",arg,'\n')
            sleep(10)
            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()
    
        view.setResizeMode(QQuickView.SizeRootObjectToView)
    
        myModel = PersonModel()
        myModel.populate()
    
        print(myModel._data)
    
        view.rootContext().setContextProperty("myModel", myModel)
        view.setSource('view.qml')
    
        thread1 = Thread(target=function01, args=(5, myModel, 'thread1',))
        thread1.start()
        # thread1.join()
        # thread2.join()
    
        view.show()
        app.exec_()
        # 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()
                }
            }
        }
    }
    
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved