QTableView 中 Drop-Events 的事件过滤器
EventFilter for Drop-Events within QTableView
我尝试创建一个可以处理放置事件的 QTableView。出于应用程序架构的原因,我希望通过我的 eventFilter 来完成(它也处理一些 QAction-triggers 用于剪贴板交互)。但是 drop-event 似乎没有通过 eventFilter。
我不关心数据在视图中的放置位置。这是我希望它由那个 eventFilter 而不是由模型处理的原因之一。此外,我不希望模型弹出对话框 ("Are you sure to drop so many elements?"),因为用户交互应该由 gui 元素完成。
顺便说一句:确实在Qt4/PySide.
中确实有效
我设置了一个示例代码片段来说明问题。有趣的是,可以出现QDrop Events,但只出现在item view的headers。
#!/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?
view.viewport()
获取所有剩余事件。所以只需 添加
view.viewport().installEventFilter(filter)
会做。
我尝试创建一个可以处理放置事件的 QTableView。出于应用程序架构的原因,我希望通过我的 eventFilter 来完成(它也处理一些 QAction-triggers 用于剪贴板交互)。但是 drop-event 似乎没有通过 eventFilter。
我不关心数据在视图中的放置位置。这是我希望它由那个 eventFilter 而不是由模型处理的原因之一。此外,我不希望模型弹出对话框 ("Are you sure to drop so many elements?"),因为用户交互应该由 gui 元素完成。
顺便说一句:确实在Qt4/PySide.
中确实有效我设置了一个示例代码片段来说明问题。有趣的是,可以出现QDrop Events,但只出现在item view的headers。
#!/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?
view.viewport()
获取所有剩余事件。所以只需 添加
view.viewport().installEventFilter(filter)
会做。