如何在 PyQt 应用程序退出时禁用清除剪贴板?
我有一个简单的 PyQt4 应用程序(请参阅下面的代码),它揭示了一个问题:如果我从 QLineEdit
选择文本并将其复制到剪贴板,那么我只能在我的应用程序正在运行。似乎在退出时,PyQt 应用程序会清除剪贴板,因此我无法在应用程序关闭后粘贴文本。
我该怎么做才能避免这个问题?
PyQt 4.4.3 @ Python 2.5 @ Windows XP。这种效果也在 PyQt 4.5+ 和 Linux 上得到了证实。
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
edit = QtGui.QLineEdit()
edit.setText('foo bar')
edit.show()
app.exec_()
I have a simple PyQt4 application (see the code below) that reveals a problem: if I select the text from a QLineEdit
and copy it to the clipboard, then I can paste it to another application only while my application is running. It seems that on exit, PyQt application clears the clipboard so I can't paste the text after the application is closed.
What can I do to avoid this problem?
PyQt 4.4.3 @ Python 2.5 @ Windows XP. Also this effect confirmed on PyQt 4.5+, and on Linux too.
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
edit = QtGui.QLineEdit()
edit.setText('foo bar')
edit.show()
app.exec_()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,没有完全清除剪贴板的情况发生。只是 QT 在剪贴板中存储某种类型的文本指针,而不仅仅是文本。 Gordon Tyler 向我指出了 PyQt 邮件列表上的讨论解释了发生了什么事。我引用代码和解释的相关部分。
在应用程序退出时运行此代码(例如在 closeEvent 处理程序中):
上面的代码是非常跨平台的,不会对 Linux 平台产生任何不良影响。
OK, there is not exactly clear of clipboard occurs. Just QT store some sort of pointer of text in the clipboard instead of just text. Gordon Tyler has pointed me to this discussion on the PyQt mailing list which explains what's going on. I quote code and relevant part of explanation.
Run this code on exit of application (e.g. in closeEvent handler):
The code above is pretty cross-platform and don't make any bad impact on Linux platform.
当我在 GNU/Linux 下遇到类似问题时,我偶然发现了这个问题,并在 bialix 引用的网站上找到了答案(其地址已更改,但仍然可以通过网络搜索到达)。引用最相关的部分:
所以就是这样。这是 GNU/Linux 的系统问题。以下是来自 ubuntu wiki 的更全面的解释:
您可以通过安装剪贴板管理器(例如parcellite、klipper、glipper 或clipman)来解决这个问题。
I stumbled upon this question when I ran into similar issue under GNU/Linux and found the answer on the site referenced by bialix (whose address has changed, but can still be reached via web search). To cite the most relevant part:
So there's that. It's a system thing for GNU/Linux. Here's a more thorough explanation from ubuntu wiki:
You can get around it by installing a clipboard manager such as parcellite, klipper, glipper or clipman.