使用Pyside关闭QTEXTEDIT对象的粘贴6
我正在尝试关闭 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应用参数或属性的函数的名称通常以
set
。开始
canpaste()剪贴板的内容可用于在文本编辑中粘贴。
解决方案是覆盖
insertfrommimimeData() >,每次都尝试粘贴 content时都称为:
请注意,请注意上下文菜单仍然会显示“粘贴”动作,因此您可能应该禁用该操作可以通过覆盖
caninsertertfrommimimimimimedata()始终返回false:
不过,请注意,这将完全损害通过剪贴板内部编辑的默认行为,包括拖动文本选择以将其移至另一个位置。
一种可能的替代方法是覆盖
createmimimimimimimimimimimimimimimimimedatatataatafromselection() a>,获取默认实现的QMIMEDATA并添加自定义格式,以此方式,我们可以在
insertfromimimedata()
和caninsertFrommimimeData()
中检查insertfromimimedata()是在内部是否生成,最终接受或忽略它。
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: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: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 ininsertFromMimeData()
andcanInsertFromMimeData()
whether the "pasted" data is generated internally or not and eventually accept or ignore it.