如何在 PyQt 应用程序退出时禁用清除剪贴板?

发布于 2024-08-16 17:25:56 字数 416 浏览 2 评论 0原文

我有一个简单的 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 技术交流群。

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

发布评论

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

评论(2

得不到的就毁灭 2024-08-23 17:25:56

好的,没有完全清除剪贴板的情况发生。只是 QT 在剪贴板中存储某种类型的文本指针,而不仅仅是文本。 Gordon Tyler 向我指出了 PyQt 邮件列表上的讨论解释了发生了什么事。我引用代码和解释的相关部分。

在应用程序退出时运行此代码(例如在 closeEvent 处理程序中):

   from PyQt4 import QtGui, QtCore
   clipboard = QtGui.QApplication.clipboard()
   event = QtCore.QEvent(QtCore.QEvent.Clipboard)
   QtGui.QApplication.sendEvent(clipboard, event)

这背后的基本概念是默认情况下复制某些内容
到剪贴板中仅复制指向源的引用/指针
应用。然后当另一个应用程序想要粘贴数据时
它从剪贴板请求源应用程序的数据。
调用 OleFlushClipboard 会导致 Windows 复制真实数据
放入剪贴板而不是引用。虽然这确实会导致
复制图像时出现延迟,应该不会产生任何明显的影响
带字符串。

上面的代码是非常跨平台的,不会对 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):

   from PyQt4 import QtGui, QtCore
   clipboard = QtGui.QApplication.clipboard()
   event = QtCore.QEvent(QtCore.QEvent.Clipboard)
   QtGui.QApplication.sendEvent(clipboard, event)

The basic concept behind this is that by default copying something
into the clipboard only copies a reference/pointer to the source
application. Then when another application wants to paste the data
from the clipboard it requests the data from the source application.
Calling OleFlushClipboard causes Windows to copy the real data
into the clipboard instead of the reference. While this does cause a
delay when copying images, it should not have any noticeable impact
with strings.

The code above is pretty cross-platform and don't make any bad impact on Linux platform.

弥繁 2024-08-23 17:25:56

当我在 GNU/Linux 下遇到类似问题时,我偶然发现了这个问题,并在 bialix 引用的网站上找到了答案(其地址已更改,但仍然可以通过网络搜索到达)。引用最相关的部分:

请记住,在 Linux 中,除非您有 klipper 之类的东西,否则当设置剪贴板的应用程序退出时,剪贴板会清空。

所以就是这样。这是 GNU/Linux 的系统问题。以下是来自 ubuntu wiki 的更全面的解释:

为什么会发生这种情况?

出现此问题是因为 Xorg 采用保守的复制方法。当用户执行选择或复制时,它仅复制对原始数据的引用。在用户请求粘贴之前,它不会从源程序中检索实际数据。这样可以节省大量不必要的数据传输,但代价是无法从未将剪贴板保存在其他地方的已关闭程序中检索数据。

您可以通过安装剪贴板管理器(例如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:

Remember that in Linux, unless you have something like klipper, when the app setting the clipboard exits, the clipboard empties.

So there's that. It's a system thing for GNU/Linux. Here's a more thorough explanation from ubuntu wiki:

Why does this happen?

The problem happens because Xorg takes a conservative approach to copying. It copies only a reference to the original data when the user performs a select or copy. It doesn't go and retrieve the actual data from the source program until the user requests a paste. It saves a lot of unneeded transfer of data this way, at the expense of having no way of retrieving data from a closed program that hasn't saved its clipboard somewhere else.

You can get around it by installing a clipboard manager such as parcellite, klipper, glipper or clipman.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文