[PyQt] QML TableView and dynamic list from python

Daniel Krause madakr at web.de
Thu Mar 17 17:37:45 GMT 2016


Barry, thanks for sharing your code.

I'll try to adopt it and see if it works with QML as well.

Daniel

2016-03-17 10:45 GMT+01:00 Barry Scott <barry at barrys-emacs.org>:

> 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
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160317/49116697/attachment.html>


More information about the PyQt mailing list