How can I disable clear of clipboard on exit of PyQt application?

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.

Leave a Comment