PyQt剪贴板不会复制到系统剪贴板 [英] PyQt clipboard doesn't copy to system clipboard
问题描述
以下代码片段似乎根本不影响系统剪贴板:
The following snippet of code doesn't seem to affect the system clipboard at all:
clipboard = QtGui.QApplication.clipboard()
clipboard.setText(text)
根据Qt文档,这是将文本复制到剪贴板的方式,
According to the Qt documentation, this is how you copy text to clipboard,
为什么不起作用?
谷歌搜索已变成此。
建议在上面的代码后添加:
It suggests adding this after the above code:
event = QtCore.QEvent(QtCore.QEvent.Clipboard)
app.sendEvent(clipboard, event)
但是这一行为有点奇怪:它仅在程序退出后才将文本复制到剪贴板。另外,该链接中的某些人报告说这不适用于linux。
But this one behaves odd: it only copies the text to the clipboard after the program exits. Plus, some people in that link reported that this doesn't work with linux.
没关系,我在其他地方做错了什么,而不是将复制插槽绑定到复制按钮,而是将其连接到退出按钮。
Nevermind, I was doing something wrong else where, instead of binding the copy slot to the copy button, I connected it to the "quit" button.
推荐答案
您可能需要指定模式。
此代码在Windows上对我有用:
This code worked for me on windows:
cb = QtGui.QApplication.clipboard()
cb.clear(mode=cb.Clipboard )
cb.setText("Clipboard Text", mode=cb.Clipboard)
这篇关于PyQt剪贴板不会复制到系统剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!