[PyQt] QML TableView and dynamic list from python

Daniel Krause madakr at web.de
Wed Mar 16 13:46:58 GMT 2016


Hello,

I have a TableView in QML:

import QtQuick 2.5
import QtQuick.Controls 1.4

TableView {
    x: 0
    y: 0
    width: 400
    height: 400
    TableViewColumn{
        role: "test"
        title: "Test"
    }
    model: listModel
}

and a list defined in python, the application is started from python:

from PyQt5.QtCore import QUrl
import sys
from PyQt5.QtQuick import QQuickView
from PyQt5.QtGui import QGuiApplication

if __name__ == '__main__':
    app = QGuiApplication(sys.argv)

    view = QQuickView()
    root_context = view.rootContext()

    list_model = ['item1', 'item2']

    root_context.setContextProperty('listModel', list_model)
    view.setSource(QUrl.fromLocalFile('_test.qml'))
    view.setResizeMode(QQuickView.SizeRootObjectToView)
    view.show()

    sys.exit(app.exec_())

[image: Inline-Bild 1]

Now I want to add a new item to the list:
list_model = list_model.append('item3')

How do I propagate that change to the view in QML?

Any help is welcome.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160316/82e7400c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 3843 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160316/82e7400c/attachment.png>


More information about the PyQt mailing list