使用Pyside关闭QTEXTEDIT对象的粘贴6

发布于 2025-01-20 03:03:09 字数 290 浏览 1 评论 0原文

我正在尝试关闭 QtWidgets.QtextEdit 对象 (PySide6) 的粘贴,但对跨平台是否默认关闭感到困惑。例如,我直觉地认为以下内容可行:

class MyEditor(QtWidgets.QTextEdit):
    def _setup_interface(self):
        self.canPaste(False)

但这会导致错误,因为 QtWidgets.QTextEdit.canPaste 不接受参数。有没有办法明确关闭粘贴,或者我是否需要相信它会默认关闭?

I'm trying to turn off pasting for a QtWidgets.QtextEdit object (PySide6), but am confused about whether it's off by default across platforms. E.g., I intuitively thought the following would work:

class MyEditor(QtWidgets.QTextEdit):
    def _setup_interface(self):
        self.canPaste(False)

but this results in an error since QtWidgets.QTextEdit.canPaste doesn't take an argument. Is there a way to explicitly turn off pasting, or do I need to trust it will be off by default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2025-01-27 03:03:09

应用参数或属性的函数的名称通常以set
开始
canpaste()剪贴板的内容可用于在文本编辑中粘贴。

解决方案是覆盖 insertfrommimimeData() >,每次都尝试粘贴 content时都称为:

class NoPasteTextEdit(QtWidgets.QTextEdit):
    def insertFromMimeData(self, source):
        pass

请注意,请注意上下文菜单仍然会显示“粘贴”动作,因此您可能应该禁用该操作可以通过覆盖 caninsertertfrommimimimimimedata()始终返回false:

    def canInsertFromMimeData(self, source):
        return False

不过,请注意,这将完全损害通过剪贴板内部编辑的默认行为,包括拖动文本选择以将其移至另一个位置。

一种可能的替代方法是覆盖 createmimimimimimimimimimimimimimimimimedatatataatafromselection() a>,获取默认实现的QMIMEDATA并添加自定义格式,以此方式,我们可以在insertfromimimedata()caninsertFrommimimeData()中检查insertfromimimedata()是在内部是否生成,最终接受或忽略它。

class NoPasteTextEdit(QtWidgets.QTextEdit):
    def canInsertFromMimeData(self, source):
        return source.hasFormat('InternalClipboard')

    def insertFromMimeData(self, source):
        if source.hasFormat('InternalClipboard'):
            super().insertFromMimeData(source)

    def createMimeDataFromSelection(self):
        # the mime object returned from the default implementation does
        # not allow to set further arbitrary data, so we just create a
        # standard QMimeData and fill it with the existing data
        mime = QtCore.QMimeData()
        src = super().createMimeDataFromSelection()
        for fmt in src.formats():
            mime.setData(fmt, src.data(fmt))
        mime.setData('InternalClipboard', QtCore.QByteArray())
        return mime

Names of functions that apply parameters or properties normally start with a set.
canPaste() just tells if the content of the clipboard can be used to paste in the text edit.

The solution is to override insertFromMimeData(), which is called every time there is an attempt to paste content, and just do nothing:

class NoPasteTextEdit(QtWidgets.QTextEdit):
    def insertFromMimeData(self, source):
        pass

Note that the context menu will still show the "Paste" action, so you should probably disable that, which can be achieved by overriding canInsertFromMimeData() and always returning False:

    def canInsertFromMimeData(self, source):
        return False

Be aware, though, that this will completely compromise the default behavior of internal editing through clipboard, including dragging a text selection to move it to another position.

A possible alternative is to override createMimeDataFromSelection(), get the QMimeData of the default implementation and add a custom format, in this way we can check in insertFromMimeData() and canInsertFromMimeData() whether the "pasted" data is generated internally or not and eventually accept or ignore it.

class NoPasteTextEdit(QtWidgets.QTextEdit):
    def canInsertFromMimeData(self, source):
        return source.hasFormat('InternalClipboard')

    def insertFromMimeData(self, source):
        if source.hasFormat('InternalClipboard'):
            super().insertFromMimeData(source)

    def createMimeDataFromSelection(self):
        # the mime object returned from the default implementation does
        # not allow to set further arbitrary data, so we just create a
        # standard QMimeData and fill it with the existing data
        mime = QtCore.QMimeData()
        src = super().createMimeDataFromSelection()
        for fmt in src.formats():
            mime.setData(fmt, src.data(fmt))
        mime.setData('InternalClipboard', QtCore.QByteArray())
        return mime
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文