[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] QML TableView and dynamic list from python
From: Daniel Krause <madakr () web ! de>
Date: 2016-03-17 17:37:45
Message-ID: CAMiLiKhhDj9A7ktwQPwTfJHva83JQ9R6TJfc6X2Q-rGYoKrPAA () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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@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@riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
[Attachment #5 (text/html)]
<div dir="ltr">Barry, thanks for sharing your code.<div><br></div><div>I'll try \
to adopt it and see if it works with QML as \
well.</div><div><br></div><div>Daniel</div></div><div class="gmail_extra"><br><div \
class="gmail_quote">2016-03-17 10:45 GMT+01:00 Barry Scott <span dir="ltr"><<a \
href="mailto:barry@barrys-emacs.org" \
target="_blank">barry@barrys-emacs.org</a>></span>:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Wednesday 16 March \
2016 14:46:58 Daniel Krause wrote:<br> > Hello,<br>
><br>
> I have a TableView in QML:<br>
><br>
> import QtQuick 2.5<br>
> import QtQuick.Controls 1.4<br>
><br>
> TableView {<br>
> x: 0<br>
> y: 0<br>
> width: 400<br>
> height: 400<br>
> TableViewColumn{<br>
> role: "test"<br>
> title: "Test"<br>
> }<br>
> model: listModel<br>
> }<br>
><br>
> and a list defined in python, the application is started from python:<br>
><br>
> from PyQt5.QtCore import QUrl<br>
> import sys<br>
> from PyQt5.QtQuick import QQuickView<br>
> from PyQt5.QtGui import QGuiApplication<br>
><br>
> if __name__ == '__main__':<br>
> app = QGuiApplication(sys.argv)<br>
><br>
> view = QQuickView()<br>
> root_context = view.rootContext()<br>
><br>
> list_model = ['item1', 'item2']<br>
><br>
> root_context.setContextProperty('listModel', list_model)<br>
> view.setSource(QUrl.fromLocalFile('_test.qml'))<br>
> view.setResizeMode(QQuickView.SizeRootObjectToView)<br>
> view.show()<br>
><br>
> sys.exit(app.exec_())<br>
><br>
> [image: Inline-Bild 1]<br>
><br>
> Now I want to add a new item to the list:<br>
> list_model = list_model.append('item3')<br>
><br>
> How do I propagate that change to the view in QML?<br>
<br>
</div></div>I have not use QML but I have a been working with QTableView a lot \
recently.<br> <br>
I found that if I did not implement a QAbstractTableModel and update by<br>
calling the right methods the view will not update correctly. See<br>
beginInsertRows/beingRemoveRows and the dataChanged signal.<br>
<br>
You can see my code for the table model here:<br>
<br>
<a href="https://github.com/barry-scott/git-workbench/blob/master/Source/wb_git_table_model.py" \
rel="noreferrer" target="_blank">https://github.com/barry-scott/git-workbench/blob/master/Source/wb_git_table_model.py</a><br>
<br>
Barry<br>
<br>
_______________________________________________<br>
PyQt mailing list <a \
href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a></blockquote></div><br></div>
[Attachment #6 (text/plain)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic