<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 "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.</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'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'll get an error if you'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 <<a href="mailto:gottfried.mueller@gmx.de">gottfried.mueller@gmx.de</a>> 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 "5" as a child of item "1" 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 "6" for example as a sibling of items "0" and <br>
"4". 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("Start move", 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('mv_01: item={} -> parent={}'.format(srcItem.text(), <br>
dstParent.text()))<br>
         srcParentIdx = self.mdl.indexFromItem(srcItem.parent())<br>
         dstParentIdx = self.mdl.indexFromItem(dstParent)<br>
         print('idxValid_01:', srcParentIdx.isValid(), <br>
dstParentIdx.isValid())<br>
         print('rows_01:', srcItem.row(), dstParent.rowCount())<br>
         rc = self.mdl.moveRow(<br>
             srcParentIdx, srcItem.row(), dstParentIdx, dstParent.rowCount()<br>
         )<br>
         print('rc_01:', 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__ == "__main__":<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. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>