如何在PyQt应用程序退出时禁用清除剪贴板? [英] How can I disable clear of clipboard on exit of PyQt application?

查看:123
本文介绍了如何在PyQt应用程序退出时禁用清除剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的PyQt4应用程序(请参见下面的代码),该应用程序揭示了一个问题:如果我从 QLineEdit 中选择文本并将其复制到剪贴板,则我只能在我的应用程序运行时将其粘贴到另一个应用程序。似乎在退出时,PyQt应用程序清除了剪贴板,因此在应用程序关闭后我无法粘贴文本。

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.

如何避免此问题?

PyQt 4.4.3 @ Python 2.5 @ Windows XP。

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_()


推荐答案

好的,还不清楚剪贴板发生。只是QT在剪贴板中存储了某种文本指针,而不仅仅是文本。戈登·泰勒(Gordon Tyler)向我指出了有关PyQt邮件列表的讨论解释发生了什么。我引用代码和解释的相关部分。

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.

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

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)




基本概念这是因为默认情况下,将
复制到剪贴板仅会将引用/指针复制到源
应用程序。然后,当另一个应用程序想要从剪贴板粘贴数据
时,它将向源应用程序请求数据。
调用 OleFlushClipboard 导致Windows将实际数据
复制到剪贴板而不是引用中。虽然这样做确实会导致
的图像复制延迟,但它对字符串
的影响不大。

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.

上面的代码是非常跨平台的,不会对Linux平台造成任何不良影响。

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

这篇关于如何在PyQt应用程序退出时禁用清除剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆