我将QTableWidgetItem
或QTreeWidgetItem
拖到QTreeWidgetItem
上。如何获取拖放的拖动对象以及如何获取目标QTreeWidgetItem
?
这就是我所拥有的(见#COMMENTS
):
import sys
from PyQt4 import QtCore, QtGui
class TreeWidget(QtGui.QTreeWidget):
def __init__(self, parent=None):
QtGui.QTreeWidget.__init__(self, parent)
self.header().setHidden(True)
self.setSelectionMode(self.ExtendedSelection)
self.setDragEnabled(True)
self.setAcceptDrops(True)
self.setDropIndicatorShown(True)
self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.itemDropped = QtCore.pyqtSignal()
item1 = QtGui.QTreeWidgetItem(self, ['first tree folder'])
item1.setData(1, QtCore.Qt.EditRole, 'first tree data')
item2 = QtGui.QTreeWidgetItem(item1, ['second tree folder'])
item2.setData(1, QtCore.Qt.EditRole, 'second tree data')
item3 = QtGui.QTreeWidgetItem(self, ['third tree folder'])
item3.setData(1, QtCore.Qt.EditRole, 'third tree data')
item1.setExpanded(True)
def dropEvent(self, event):
"""On drop, get the target and the dragged item(s)"""
print 'drop!'
if (event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist')):
source_widget = event.source()
# HOW TO GET DESTINATION HERE?
if isinstance(source_widget, QtGui.QTreeWidget):
# HOW TO GET DRAGGED ITEMS HERE?
if isinstance(source_widget, QtGui.QTableWidget):
# HOW TO GET DRAGGED ITEMS HERE?
event.accept()
else:
event.ignore()
def dragEnterEvent(self, event):
"""Allow dragging from the tree and the table widgets"""
if (event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist')):
source_widget = event.source()
if isinstance(source_widget, QtGui.QTreeWidget):
event.accept() # Move!
if isinstance(source_widget, QtGui.QTableWidget):
event.accept()
else:
event.ignore()
class Table(QtGui.QTableWidget):
def __init__(self, parent=None):
QtGui.QTableWidget.__init__(self, parent)
self.setSelectionMode(self.ExtendedSelection)
self.setDragEnabled(True)
self.setAcceptDrops(False)
item1 = QtGui.QTableWidgetItem()
item1.setText('first table file')
item1.setData(QtCore.Qt.UserRole, 'first table data')
item2 = QtGui.QTableWidgetItem()
item2.setText('second table file')
item2.setData(QtCore.Qt.UserRole, 'second table data')
item3 = QtGui.QTableWidgetItem()
item3.setText('third file')
item3.setData(QtCore.Qt.UserRole, 'third table data')
self.setRowCount(3)
self.setColumnCount(1)
self.setItem(0, 0, item1)
self.setItem(1, 0, item2)
self.setItem(2, 0, item3)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.main_layout = QtGui.QVBoxLayout()
self.setLayout(self.main_layout)
self.tree_widget = TreeWidget()
self.table = Table()
self.main_layout.addWidget(QtGui.QLabel('Folders'))
self.main_layout.addWidget(self.tree_widget)
self.main_layout.addWidget(QtGui.QLabel('Files'))
self.main_layout.addWidget(self.table)
self.show()
def main():
app = QtGui.QApplication([])
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
答案 0 :(得分:0)
这就是我最终解决的问题:
def dropEvent(self, event):
"""On drop, get the target and the dragged item(s)"""
print 'drop!'
if (event.mimeData().hasFormat('application/x-qabstractitemmodeldatalist')):
source_widget = event.source()
destination = self.itemAt(event.pos())
if isinstance(source_widget, QtGui.QTreeWidget):
selected_items = source_widget.selectedItems()
if isinstance(source_widget, QtGui.QTableWidget):
selected_items = []
indexes = source_widget.selectionModel().selectedRows()
column = 0
for index in sorted(indexes):
selected_items.append(source_widget.item(index.row(), column))
event.accept()
else:
event.ignore()