我在QTreeWidget中有简单的树(我使用pyqt,但我觉得它并不重要)。当我在这个树中移动项目时,我想要更改数据库,所以我希望在移动之前获取此项目对象,此项目的父项以及此项目的父项。但我不明白我是如何得到它们的。 qtreewidget没有特殊的拖放信号。
答案 0 :(得分:0)
我找到了我的情况的答案。可能有些人可能感兴趣:
class Ui_MainWindow(object): # automatically generated with pyuic5
def setupUi(self, MainWindow):
...
self.recordsTree = QtWidgets.QTreeWidget(self.centralWidget)
...
class CustomTree(object):
def startDrag(self, actions):
row = self.selectedItems()[0]
self._prev_parent = row.parent().data(0, 0)
self._what_move = row.data(0,0)
return QTreeWidget.startDrag(self, actions)
def dropEvent(self, event):
row_index = self.indexAt(event.pos()).row()
self._new_parent = self.topLevelItem(row_index).data(0, 0)
my_custom_collback(self._prev_parent, self._what_move, self._new_parent) # <- that what i needed
return QTreeWidget.dropEvent(self, event)
class MainWindow(Ui_MainWindow):
def setupUi(self, m):
r = super(MainWindow, self).setupUi(m)
self.recordsTree.startDrag = types.MethodType(CustomTree.startDrag, self.recordsTree)
self.recordsTree.dropEvent = types.MethodType(CustomTree.dropEvent, self.recordsTree)
return r