gpt4 book ai didi

python - QTableView 中 Drop-Events 的 EventFilter

转载 作者:行者123 更新时间:2023-11-28 18:32:05 32 4
gpt4 key购买 nike

我尝试创建一个可以处理放置事件的 QTableView。出于应用程序架构的原因,我希望通过我的 eventFilter 来完成(它也处理一些用于剪贴板交互的 QAction 触发器)。但是 drop-event 似乎没有通过 eventFilter。

我不关心数据在 View 中的放置位置。这是我希望它由那个 eventFilter 而不是由模型处理的原因之一。此外,我不希望模型弹出一个对话框(“你确定要删除这么多元素吗?”),因为用户交互应该由 gui 元素完成。

顺便说一句:它确实在 Qt4/PySide 中确实有效。

我设置了一个示例代码片段来说明问题。有趣的是,可以出现 QDrop 事件,但只出现在项目 View 的标题中。

#!/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)

会做。

关于python - QTableView 中 Drop-Events 的 EventFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36041080/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com