• Uncategorized

About python : How-can-I-disable-clear-of-clipboard-on-exit-of-PyQt-application

Question Detail

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')

Question Answer

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.

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.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.