我试图创建一个可以处理drop事件的QTableView。出于应用程序体系结构的原因,我希望由我的一个eventFilter来完成(它也处理一些QAction-触发剪贴板交互)。但是,drop事件似乎无法通过eventFilter。
我不关心数据放在视图中的位置。这就是我希望由eventFilter而不是模型来处理它的原因之一。此外,我不希望模型弹出一个对话框(“您一定要删除这么多元素吗?”),因为用户交互应该由gui元素完成。
顺便说一句: It 确实在Qt4/PySide中使用了。
我设置了一个示例代码片段来说明这个问题。有趣的是,可以出现QDrop事件,但只能出现在项视图的标题中。
#!/usr/bin/env python2.7
# coding: utf-8
from PyQt5.QtWidgets import (
QApplication,
QMainWindow,
QTableView,
QWidget,
)
from PyQt5.QtCore import (
Qt,
QStringListModel,
QEvent
)
app = QApplication([])
window = QMainWindow()
# Table View with Drop-Options
view = QTableView(window)
view.setDropIndicatorShown(True)
view.setDragEnabled(True)
view.setAcceptDrops(True)
view.setDragDropMode(QTableView.DragDrop)
view.setDefaultDropAction(Qt.LinkAction)
view.setDropIndicatorShown(True)
window.setCentralWidget(view)
# Simple Event Filter for TableView
class Filter(QWidget):
def eventFilter(self, widget, event):
print widget, event, event.type()
if event.type() in (QEvent.DragEnter, QEvent.DragMove, QEvent.Drop):
print "Drag'n'Drop-Event"
if event.type() != QEvent.Drop:
print "\tbut no DropEvent"
event.acceptProposedAction()
else:
print "\tan actual DropEvent"
return True
return False
filter = Filter(window)
view.installEventFilter(filter)
class MyModel(QStringListModel):
# Model with activated DragDrop functionality
# vor view
def supportedDragActions(self):
print "asks supported drop actions"
return Qt.LinkAction | Qt.CopyAction
def canDropMimeData(self, *args):
print "canDropMimeData"
return True
def dropMimeData(self, *args):
print "dropMimeData"
return True
model = MyModel("Entry_A Entry_B Entry_C".split())
view.setModel(model)
window.show()
window.raise_()
app.exec_()的最后一个问题:--在QTableView中处理QDropEvents的小部件是什么,或者应该在什么小部件上安装eventFilter?
发布于 2016-03-16 17:47:43
view.viewport()获取所有剩余的事件。所以简单地说添加了
view.viewport().installEventFilter(filter)就行了。
https://stackoverflow.com/questions/36041080
复制相似问题