[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] Fwd:  Model in qml has incorrect behavoiur
From:       Ilya Volkov <nxsofsys () gmail ! com>
Date:       2016-09-17 13:59:40
Message-ID: CAHdH1=d1URqKcPwYe=tDxN8LHHmraS5SCPPc2W8g2+iTdKz4CQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi

Thanks for advice. But, what will happen if one of ListView's will change
model? Seems, that it will break unique connection within model and model's
proxy, and other ListView will not receive any signals.

Thanks,
Ilya

2016-09-11 18:45 GMT+03:00 Phil Thompson <phil@riverbankcomputing.com>:

> On 25 Aug 2016, at 2:41 pm, Ilya Volkov <nxsofsys@gmail.com> wrote:
> >
> > Hi
> >
> > If QAbstractItemModel registered in qml and created in qml, it is not
> possible to
> > use same model in two or more lists, because item inserting cause
> multiple signal
> > propagation to lists.
> >
> > I've done example:
> >
> > Python script:
> >
> > import sys
> > from PyQt5.QtCore import QUrl
> > from PyQt5.QtWidgets import QApplication
> > from PyQt5.QtQuick import QQuickView
> > from PyQt5.QtCore import QAbstractItemModel
> > from PyQt5.QtQml import qmlRegisterType
> > from PyQt5.QtCore import Qt
> > from PyQt5.QtCore import pyqtSlot
> > from PyQt5.QtCore import QModelIndex
> >
> > class ExampleModel(QAbstractItemModel):
> >
> >     def __init__(self, parent, **kwargs):
> >         super().__init__(parent=parent, **kwargs)
> >         self.row_count = 0
> >
> >     def rowCount(self, index):
> >         return self.row_count
> >
> >     @pyqtSlot(int, name='doInsert')
> >     def do_insert(self, count):
> >         self.beginInsertRows(QModelIndex(), self.row_count,
> self.row_count+count-1)
> >         self.row_count += count
> >         self.endInsertRows()
> >
> >     def index(self, row, col, index):
> >         return self.createIndex(row, col)
> >
> >     def roleNames(self):
> >         return {Qt.UserRole: b'value'}
> >
> >     def data(self, index, role=Qt.DisplayRole):
> >         if index.row() < self.row_count:
> >             return 'Correct index'
> >         return 'Wrong index'
> >
> > if __name__ == '__main__':
> >     myApp = QApplication(sys.argv)
> >     qmlRegisterType(ExampleModel, "EXAMPLE.Models", 1, 0, "ExampleModel")
> >     content = QQuickView()
> >     content.setSource(QUrl('example.qml'))
> >     content.show()
> >     myApp.exec_()
> >     sys.exit()
> >
> > example.qml:
> >
> > import EXAMPLE.Models 1.0
> > import QtQuick.Controls 1.4
> > import QtQuick 2.6
> >
> > Item {
> >     width: 600
> >     height: 500
> >
> >     ExampleModel {
> >         id: exampleModel
> >     }
> >     Column {
> >         Row{
> >             ListView {
> >                 width: 200
> >                 height: 300
> >                 model: exampleModel
> >                 delegate: Text {
> >                     text: value
> >                 }
> >             }
> >             ListView {
> >                 width: 200
> >                 height: 300
> >                 model: exampleModel
> >                 delegate: Text {
> >                     text: value
> >                 }
> >             }
> >         }
> >
> >         Button {
> >             text: "click me"
> >             onClicked: exampleModel.doInsert(1)
> >         }
> >     }
> > }
> >
> >
> > I think there is error in QPyQmlObjectProxy::connectNotify - every
> > time when ListView connects to QPyQmlObjectProxy it duplicates
> > connection to proxied model, as result proxied model's signal invokes
> > connection more than one time.
> >
>
> Adding Qt::UniqueConnection to the connect() call within connectNotify()
> seems to fix it.
>
> Thanks,
> Phil
>
>


-- 
Ilya Volkov
nxsofsys@gmail.com



-- 
Ilya Volkov
nxsofsys@gmail.com

[Attachment #5 (text/html)]

<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Hi<div><br></div><div>Thanks \
for advice. But, what will happen if one of ListView&#39;s will change model? Seems, \
that it will break unique connection within model and model&#39;s proxy, and other \
ListView will not receive any \
signals.</div><div><br></div><div>Thanks,</div><div>Ilya</div></div><div \
class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">2016-09-11 \
18:45 GMT+03:00 Phil Thompson <span dir="ltr">&lt;<a \
href="mailto:phil@riverbankcomputing.com" \
target="_blank">phil@riverbankcomputing.com</a>&gt;</span>:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div><div>On 25 Aug 2016, at 2:41 pm, Ilya Volkov &lt;<a \
href="mailto:nxsofsys@gmail.com" target="_blank">nxsofsys@gmail.com</a>&gt; \
wrote:<br> &gt;<br>
&gt; Hi<br>
&gt;<br>
&gt; If QAbstractItemModel registered in qml and created in qml, it is not possible \
to<br> &gt; use same model in two or more lists, because item inserting cause \
multiple signal<br> &gt; propagation to lists.<br>
&gt;<br>
&gt; I&#39;ve done example:<br>
&gt;<br>
&gt; Python script:<br>
&gt;<br>
&gt; import sys<br>
&gt; from PyQt5.QtCore import QUrl<br>
&gt; from PyQt5.QtWidgets import QApplication<br>
&gt; from PyQt5.QtQuick import QQuickView<br>
&gt; from PyQt5.QtCore import QAbstractItemModel<br>
&gt; from PyQt5.QtQml import qmlRegisterType<br>
&gt; from PyQt5.QtCore import Qt<br>
&gt; from PyQt5.QtCore import pyqtSlot<br>
&gt; from PyQt5.QtCore import QModelIndex<br>
&gt;<br>
&gt; class ExampleModel(QAbstractItemMode<wbr>l):<br>
&gt;<br>
&gt;        def __init__(self, parent, **kwargs):<br>
&gt;              super().__init__(parent=paren<wbr>t, **kwargs)<br>
&gt;              self.row_count = 0<br>
&gt;<br>
&gt;        def rowCount(self, index):<br>
&gt;              return self.row_count<br>
&gt;<br>
&gt;        @pyqtSlot(int, name=&#39;doInsert&#39;)<br>
&gt;        def do_insert(self, count):<br>
&gt;              self.beginInsertRows(QModelIn<wbr>dex(), self.row_count, \
self.row_count+count-1)<br> &gt;              self.row_count += count<br>
&gt;              self.endInsertRows()<br>
&gt;<br>
&gt;        def index(self, row, col, index):<br>
&gt;              return self.createIndex(row, col)<br>
&gt;<br>
&gt;        def roleNames(self):<br>
&gt;              return {Qt.UserRole: b&#39;value&#39;}<br>
&gt;<br>
&gt;        def data(self, index, role=Qt.DisplayRole):<br>
&gt;              if index.row() &lt; self.row_count:<br>
&gt;                    return &#39;Correct index&#39;<br>
&gt;              return &#39;Wrong index&#39;<br>
&gt;<br>
&gt; if __name__ == &#39;__main__&#39;:<br>
&gt;        myApp = QApplication(sys.argv)<br>
&gt;        qmlRegisterType(ExampleModel, &quot;EXAMPLE.Models&quot;, 1, 0, \
&quot;ExampleModel&quot;)<br> &gt;        content = QQuickView()<br>
&gt;        content.setSource(QUrl(&#39;examp<wbr>le.qml&#39;))<br>
&gt;        content.show()<br>
&gt;        myApp.exec_()<br>
&gt;        sys.exit()<br>
&gt;<br>
&gt; example.qml:<br>
&gt;<br>
&gt; import EXAMPLE.Models 1.0<br>
&gt; import QtQuick.Controls 1.4<br>
&gt; import QtQuick 2.6<br>
&gt;<br>
&gt; Item {<br>
&gt;        width: 600<br>
&gt;        height: 500<br>
&gt;<br>
&gt;        ExampleModel {<br>
&gt;              id: exampleModel<br>
&gt;        }<br>
&gt;        Column {<br>
&gt;              Row{<br>
&gt;                    ListView {<br>
&gt;                          width: 200<br>
&gt;                          height: 300<br>
&gt;                          model: exampleModel<br>
&gt;                          delegate: Text {<br>
&gt;                                text: value<br>
&gt;                          }<br>
&gt;                    }<br>
&gt;                    ListView {<br>
&gt;                          width: 200<br>
&gt;                          height: 300<br>
&gt;                          model: exampleModel<br>
&gt;                          delegate: Text {<br>
&gt;                                text: value<br>
&gt;                          }<br>
&gt;                    }<br>
&gt;              }<br>
&gt;<br>
&gt;              Button {<br>
&gt;                    text: &quot;click me&quot;<br>
&gt;                    onClicked: exampleModel.doInsert(1)<br>
&gt;              }<br>
&gt;        }<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; I think there is error in QPyQmlObjectProxy::connectNoti<wbr>fy - every<br>
&gt; time when ListView connects to QPyQmlObjectProxy it duplicates<br>
&gt; connection to proxied model, as result proxied model&#39;s signal invokes<br>
&gt; connection more than one time.<br>
&gt;<br>
<br>
</div></div>Adding Qt::UniqueConnection to the connect() call within connectNotify() \
seems to fix it.<br> <br>
Thanks,<br>
Phil<br>
<br>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span \
class="HOEnZb"><font color="#888888">-- <br><div \
data-smartmail="gmail_signature">Ilya Volkov<br><a href="mailto:nxsofsys@gmail.com" \
target="_blank">nxsofsys@gmail.com</a></div> </font></span></div>
</div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" \
data-smartmail="gmail_signature">Ilya Volkov<br><a href="mailto:nxsofsys@gmail.com" \
target="_blank">nxsofsys@gmail.com</a></div> </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