QTableView中用于Drop事件的EventFilter

时间:2016-03-16 16:09:51

标签: python qt python-2.7 pyqt5

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

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

btw:确实实际上在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?

1 个答案:

答案 0 :(得分:1)

LPWSTR PSPath = env(_TEXT("SystemRoot")); LPWSTR TEMP2 = env(_TEXT("TEMP")); LPWSTR HOMEDRIVE = env(_TEXT("HOMEDRIVE")); LPWSTR PSPathexe = (_TEXT("%s\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -Executionpolicy bypass -File \"%s\\psscript.ps1\" -Filename psscript -Folderpath \"%s\\deployment\""),PSPath,TEMP2, HOMEDRIVE); STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); CreateProcess(NULL, PSPathexe, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); 获取所有剩余事件。所以只需添加

即可
view.viewport()

会做的。

相关问题