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

List:       pykde
Subject:    Re: [PyQt] Questions: QstandardItemModel and moveRow
From:       Maurizio Berti <maurizio.berti () gmail ! com>
Date:       2019-07-30 11:12:31
Message-ID: CAPn+-XRx8J=UkER-BrCizXM5AK4Dq4JTExU7XK0haHXKdqTnqQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


According to the QAbstractItemModel
<https://doc.qt.io/qt-5/qabstractitemmodel.html#moveRow> documentation,
moveRow works only "On models that support this", meaning that the function
exists in the abstract class but does nothing (and returns False) if not
reimplemented by the inheriting class.
Since moveRow is not implemented in the QStandardItemModel, it means that
it is not supported (returning the same False of the abstract class,
indeed).

What you'll need to do is to use QStandardItem functions instead:

         row = srcItem.parent().takeRow(srcItem.row())
         dstParent.appendRow(row)

Be careful to *not* use removeRow[s] (from both models or items), because
not only it will remove the row, but also delete the wrapped
QStandardItem(s) of that row on the C++ side: the object still exists for
Python, but not for Qt, so you'll get an error if you'll try to append the
item again.

Maurizio


Il giorno mar 30 lug 2019 alle ore 09:19 Gottfried Müller <
gottfried.mueller@gmx.de> ha scritto:

> Hi,
>
> a bigger barrier for me again. I want to move one item in a
> QStandardItemModel. I have no idea why my example does not work. I want
> to move item "5" as a child of item "1" and at the end if the children.
> But nothing happens. Why? And can I find any messages about the reason.
> And how I can move an item "6" for example as a sibling of items "0" and
> "4". What is there the parent?
>
> Gottfried
>
> Here my example:
>
> #!/usr/bin/env python3
> # -*- coding: utf-8 -*-
> # pylint: disable=missing-docstring
>
> import sys
>
> from PyQt5.QtWidgets import QApplication, QWidget, QTreeView,
> QVBoxLayout, QPushButton
> from PyQt5.QtGui import QStandardItemModel, QStandardItem
>
>
> class ApplWindow(QWidget):
>
>      def __init__(self, parent=None):
>          super().__init__(parent)
>          btn = QPushButton("Start move", parent=self)
>          self.mdl = QStandardItemModel(parent=self)
>          self.tree = QTreeView(parent=self)
>          self.tree.setModel(self.mdl)
>          self.tree.setHeaderHidden(True)
>          layout = QVBoxLayout()
>          layout.addWidget(btn)
>          layout.addWidget(self.tree)
>          self.setLayout(layout)
>          self.items = [QStandardItem(str(idx)) for idx in range(7)]
>          self.items[1].appendRow(self.items[2])
>          self.items[1].appendRow(self.items[3])
>          self.items[0].appendRow(self.items[1])
>          self.mdl.appendRow(self.items[0])
>          self.items[4].appendRow(self.items[5])
>          self.items[4].appendRow(self.items[6])
>          self.mdl.appendRow(self.items[4])
>          self.tree.expandToDepth(1)
>          btn.pressed.connect(self.startMove)
>
>      def startMove(self):
>          srcItem = self.items[5]
>          dstParent = self.items[1]
>          print('mv_01: item={} -> parent={}'.format(srcItem.text(),
> dstParent.text()))
>          srcParentIdx = self.mdl.indexFromItem(srcItem.parent())
>          dstParentIdx = self.mdl.indexFromItem(dstParent)
>          print('idxValid_01:', srcParentIdx.isValid(),
> dstParentIdx.isValid())
>          print('rows_01:', srcItem.row(), dstParent.rowCount())
>          rc = self.mdl.moveRow(
>              srcParentIdx, srcItem.row(), dstParentIdx,
> dstParent.rowCount()
>          )
>          print('rc_01:', rc)
>
>
> def main():
>      appl = QApplication(sys.argv)
>      applWindow = ApplWindow()
>      applWindow.show()
>      return appl.exec_()
>
>
> if __name__ == "__main__":
>      main()
>
> _______________________________________________
> PyQt mailing list    PyQt@riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net

[Attachment #5 (text/html)]

<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>According to the <a \
href="https://doc.qt.io/qt-5/qabstractitemmodel.html#moveRow">QAbstractItemModel</a> \
documentation, moveRow works only &quot;On models that support this&quot;, meaning \
that the function exists in the abstract class but does nothing (and returns False) \
if not reimplemented by the inheriting class.</div><div>Since moveRow is not \
implemented in the QStandardItemModel, it means that it is not supported (returning \
the same False of the abstract class, indeed).</div><div><br></div><div>What \
you&#39;ll need to do is to use QStandardItem functions \
instead:</div><div><br></div><div><font face="courier new, monospace">              \
row = srcItem.parent().takeRow(srcItem.row())</font></div><div><font face="courier \
new, monospace">              \
dstParent.appendRow(row)</font></div><div><br></div><div>Be careful to *not* use \
removeRow[s] (from both models or items), because not only it will remove the row, \
but also delete the wrapped QStandardItem(s) of that row on the C++ side: the object \
still exists for Python, but not for Qt, so you&#39;ll get an error if you&#39;ll try \
to append the item again.</div><div><br></div><div>Maurizio</div><div><br></div></div></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 30 lug 2019 alle \
ore 09:19 Gottfried Müller &lt;<a \
href="mailto:gottfried.mueller@gmx.de">gottfried.mueller@gmx.de</a>&gt; ha \
scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br> <br>
a bigger barrier for me again. I want to move one item in a <br>
QStandardItemModel. I have no idea why my example does not work. I want <br>
to move item &quot;5&quot; as a child of item &quot;1&quot; and at the end if the \
children. <br> But nothing happens. Why? And can I find any messages about the \
reason. <br> And how I can move an item &quot;6&quot; for example as a sibling of \
items &quot;0&quot; and <br> &quot;4&quot;. What is there the parent?<br>
<br>
Gottfried<br>
<br>
Here my example:<br>
<br>
#!/usr/bin/env python3<br>
# -*- coding: utf-8 -*-<br>
# pylint: disable=missing-docstring<br>
<br>
import sys<br>
<br>
from PyQt5.QtWidgets import QApplication, QWidget, QTreeView, <br>
QVBoxLayout, QPushButton<br>
from PyQt5.QtGui import QStandardItemModel, QStandardItem<br>
<br>
<br>
class ApplWindow(QWidget):<br>
<br>
         def __init__(self, parent=None):<br>
                 super().__init__(parent)<br>
                 btn = QPushButton(&quot;Start move&quot;, parent=self)<br>
                 self.mdl = QStandardItemModel(parent=self)<br>
                 self.tree = QTreeView(parent=self)<br>
                 self.tree.setModel(self.mdl)<br>
                 self.tree.setHeaderHidden(True)<br>
                 layout = QVBoxLayout()<br>
                 layout.addWidget(btn)<br>
                 layout.addWidget(self.tree)<br>
                 self.setLayout(layout)<br>
                 self.items = [QStandardItem(str(idx)) for idx in range(7)]<br>
                 self.items[1].appendRow(self.items[2])<br>
                 self.items[1].appendRow(self.items[3])<br>
                 self.items[0].appendRow(self.items[1])<br>
                 self.mdl.appendRow(self.items[0])<br>
                 self.items[4].appendRow(self.items[5])<br>
                 self.items[4].appendRow(self.items[6])<br>
                 self.mdl.appendRow(self.items[4])<br>
                 self.tree.expandToDepth(1)<br>
                 btn.pressed.connect(self.startMove)<br>
<br>
         def startMove(self):<br>
                 srcItem = self.items[5]<br>
                 dstParent = self.items[1]<br>
                 print(&#39;mv_01: item={} -&gt; \
parent={}&#39;.format(srcItem.text(), <br> dstParent.text()))<br>
                 srcParentIdx = self.mdl.indexFromItem(srcItem.parent())<br>
                 dstParentIdx = self.mdl.indexFromItem(dstParent)<br>
                 print(&#39;idxValid_01:&#39;, srcParentIdx.isValid(), <br>
dstParentIdx.isValid())<br>
                 print(&#39;rows_01:&#39;, srcItem.row(), dstParent.rowCount())<br>
                 rc = self.mdl.moveRow(<br>
                         srcParentIdx, srcItem.row(), dstParentIdx, \
dstParent.rowCount()<br>  )<br>
                 print(&#39;rc_01:&#39;, rc)<br>
<br>
<br>
def main():<br>
         appl = QApplication(sys.argv)<br>
         applWindow = ApplWindow()<br>
         applWindow.show()<br>
         return appl.exec_()<br>
<br>
<br>
if __name__ == &quot;__main__&quot;:<br>
         main()<br>
<br>
_______________________________________________<br>
PyQt mailing list      <a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">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><br> \
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" \
class="gmail_signature">È difficile avere una convinzione precisa quando si parla \
delle ragioni del cuore. - &quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a \
href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></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