[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&#39;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">&lt;<a \
href="mailto:barry@barrys-emacs.org" \
target="_blank">barry@barrys-emacs.org</a>&gt;</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> &gt; Hello,<br>
&gt;<br>
&gt; I have a TableView in QML:<br>
&gt;<br>
&gt; import QtQuick 2.5<br>
&gt; import QtQuick.Controls 1.4<br>
&gt;<br>
&gt; TableView {<br>
&gt;        x: 0<br>
&gt;        y: 0<br>
&gt;        width: 400<br>
&gt;        height: 400<br>
&gt;        TableViewColumn{<br>
&gt;              role: &quot;test&quot;<br>
&gt;              title: &quot;Test&quot;<br>
&gt;        }<br>
&gt;        model: listModel<br>
&gt; }<br>
&gt;<br>
&gt; and a list defined in python, the application is started from python:<br>
&gt;<br>
&gt; from PyQt5.QtCore import QUrl<br>
&gt; import sys<br>
&gt; from PyQt5.QtQuick import QQuickView<br>
&gt; from PyQt5.QtGui import QGuiApplication<br>
&gt;<br>
&gt; if __name__ == &#39;__main__&#39;:<br>
&gt;        app = QGuiApplication(sys.argv)<br>
&gt;<br>
&gt;        view = QQuickView()<br>
&gt;        root_context = view.rootContext()<br>
&gt;<br>
&gt;        list_model = [&#39;item1&#39;, &#39;item2&#39;]<br>
&gt;<br>
&gt;        root_context.setContextProperty(&#39;listModel&#39;, list_model)<br>
&gt;        view.setSource(QUrl.fromLocalFile(&#39;_test.qml&#39;))<br>
&gt;        view.setResizeMode(QQuickView.SizeRootObjectToView)<br>
&gt;        view.show()<br>
&gt;<br>
&gt;        sys.exit(app.exec_())<br>
&gt;<br>
&gt; [image: Inline-Bild 1]<br>
&gt;<br>
&gt; Now I want to add a new item to the list:<br>
&gt; list_model = list_model.append(&#39;item3&#39;)<br>
&gt;<br>
&gt; 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