QAction 快捷方式 "paste" 不触发

QAction shortcut "paste" does not trigger

我正在使用 Pyside2(在 windows 中)编写文字处理器。我有一个用于粘贴的 QAction 和一个用于此操作的快捷方式:

    toolbar = QToolBar("Toolbar")
    self.addToolBar(toolbar)

    paste_action = QAction("Paste", self)
    paste_action.setShortcut(QKeySequence.Paste)
    paste_action.triggered.connect(self.fn_paste)
    toolbar.addAction(paste_action)


def fn_paste(self):
    print("pasted")

预期的行为是,当我按下 ctrl + v(在 windows 中粘贴快捷方式)并将焦点放在 QTextEdit 上时,它应该 运行 fn_paste。相反,它将剪贴板中的文本粘贴到 QTextEdit 中,而不是 运行 fn_paste。如果我设置相同

paste_action.setShortcut(QKeySequence("CTRL+V"))

但是当我使用其他一些 KeySequence

paste_action.setShortcut(QKeySequence("CTRL+E"))

它确实按预期工作(在控制台中打印“粘贴”)。

同样的问题发生在:

paste_sc = QShortcut(QKeySequence.Paste, self)
paste_sc.activated.connect(self.fn_paste)

我猜系统快捷方式“ctrl + v”以某种方式覆盖了应用程序快捷方式? (但我不知道)。

也许我遗漏了一些东西(我几天前才开始使用 QT,但还是个菜鸟)。那么...我怎样才能让它发挥作用?

根据要求提供一个最小的、可重现的示例:

import sys

from PySide2.QtGui import (
    QKeySequence,
    )

from PySide2.QtWidgets import (
    QApplication,
    QMainWindow,
    QTextEdit,
    QToolBar,
    QAction,
    )


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        textfield = QTextEdit()
        self.setCentralWidget(textfield)

        toolbar = QToolBar("Toolbar")
        self.addToolBar(toolbar)

        paste_action = QAction("Paste", self)
        paste_action.setStatusTip("Paste from clipboard")
        paste_action.setShortcut(QKeySequence.Paste) #does not work
        # paste_action.setShortcut(QKeySequence("CTRL+V")) #does not work
        # paste_action.setShortcut(QKeySequence("CTRL+E")) #works but wrong shortcut
        paste_action.triggered.connect(self.fn_paste)
        toolbar.addAction(paste_action)

    def fn_paste(self):
        print("Pasted")


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

我现在使用事件过滤器来获得所需的结果。因为我仍然试图弄清楚它是如何工作的以及为什么工作(我只有一个粗略的想法,一些解释的帮助会很好),请对它持保留态度:

mre:

import sys

from PySide2.QtCore import (
    QEvent
    )

from PySide2.QtGui import (
    QKeySequence,
    )

from PySide2.QtWidgets import (
    QApplication,
    QMainWindow,
    QTextEdit,
    QToolBar,
    QAction,
    )


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.textfield = TextField()
        self.setCentralWidget(self.textfield)

        toolbar = QToolBar("Toolbar")
        self.addToolBar(toolbar)

        paste_action = QAction("Paste", self)
        paste_action.setShortcut(QKeySequence.Paste)
        paste_action.triggered.connect(self.fn_paste)
        toolbar.addAction(paste_action)

    def fn_paste(self):
        self.textfield.paste()
        print("Pasted")


class TextField(QTextEdit):
    def __init__(self):
        super().__init__()
        self.installEventFilter(self)

    def eventFilter(self, object, event):
        if event.type() == QEvent.ShortcutOverride:
            return True
        return False


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()