我正在开发一个 PyQt5 应用程序,其中有一个用 QStandardItemModel 填充的 QTreeView。根项目应该可以在树视图层次结构中拖动和移动。但根项目不应该直接拖放到另一个根项目上。
我尝试在 QTreeView 子类中实现自定义 dropEvent 方法,但遇到根项目无法在其他根项目上自由移动的问题。
这是我当前实现的简化版本:
from PyQt5.QtWidgets import QApplication, QTreeView, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtGui import QStandardItem, QStandardItemModel
class MyTreeView(QTreeView):
def dropEvent(self, e):
currentIndex = e.source().currentIndex()
destinationIndex = self.indexAt(e.pos())
# Check if the dragged item is a root item
if not currentIndex.parent().isValid():
# Check if the drop destination is a root item
if not destinationIndex.parent().isValid():
# Ignore the drop if both are root items
e.ignore()
return
super().dropEvent(e)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.tree_view = MyTreeView(self)
self.tree_view.setDragEnabled(True)
self.tree_view.setAcceptDrops(True)
self.tree_view.setDropIndicatorShown(True)
self.tree_view.setDragDropMode(QTreeView.InternalMove)
self.model = QStandardItemModel()
# Create sample items
root_item1 = QStandardItem("Root Item 1")
child_item11 = QStandardItem("Child Item 1.1")
child_item12 = QStandardItem("Child Item 1.2")
root_item1.appendRow(child_item11)
root_item1.appendRow(child_item12)
root_item2 = QStandardItem("Root Item 2")
child_item21 = QStandardItem("Child Item 2.1")
child_item22 = QStandardItem("Child Item 2.2")
root_item2.appendRow(child_item21)
root_item2.appendRow(child_item22)
self.model.appendRow(root_item1)
self.model.appendRow(root_item2)
self.tree_view.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(self.tree_view)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
您的意思是根不能作为另一行内的子行吗?
如果是这样,那么您可以禁用将根拖动到任何位置:
from PyQt5.QtWidgets import QApplication, QTreeView, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtGui import QStandardItem, QStandardItemModel
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.tree_view = QTreeView(self)
self.tree_view.setDragEnabled(True)
self.tree_view.setAcceptDrops(True)
self.tree_view.setDropIndicatorShown(True)
self.tree_view.setDragDropMode(QTreeView.InternalMove)
self.model = QStandardItemModel()
# Create sample items
root_item1 = QStandardItem("Root Item 1")
root_item1.setDragEnabled(False) # disable Draging the root
child_item11 = QStandardItem("Child Item 1.1")
child_item12 = QStandardItem("Child Item 1.2")
root_item1.appendRow(child_item11)
root_item1.appendRow(child_item12)
root_item2 = QStandardItem("Root Item 2")
root_item2.setDragEnabled(False) # disable Draging the root
child_item21 = QStandardItem("Child Item 2.1")
child_item22 = QStandardItem("Child Item 2.2")
root_item2.appendRow(child_item21)
root_item2.appendRow(child_item22)
self.model.insertRow(0,root_item1)
self.model.insertRow(1, root_item2)
self.tree_view.setModel(self.model)
layout = QVBoxLayout()
layout.addWidget(self.tree_view)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
试试这个。