[PyQt] QML TableView and dynamic list from python

Barry Scott barry at barrys-emacs.org
Thu Mar 17 09:45:08 GMT 2016


On Wednesday 16 March 2016 14:46:58 Daniel Krause wrote:
> 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?

I have not use QML but I have a been working with QTableView a lot recently.

I found that if I did not implement a QAbstractTableModel and update by 
calling the right methods the view will not update correctly. See 
beginInsertRows/beingRemoveRows and the dataChanged signal.

You can see my code for the table model here:

https://github.com/barry-scott/git-workbench/blob/master/Source/wb_git_table_model.py

Barry



More information about the PyQt mailing list