How can I disable the clipboard when exiting the PyQt4 application?

I have a simple PyQt4 application (see code below) that shows the following problem: if I select text from QLineEdit and copy it to the clipboard, then I can paste it into another application only while my application is running. It seems that upon exiting the PyQt application clears the clipboard, so I cannot 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 is confirmed on PyQt 4.5+, as well as on Linux.

import sys from PyQt4 import QtGui app = QtGui.QApplication(sys.argv) edit = QtGui.QLineEdit() edit.setText('foo bar') edit.show() app.exec_() 
+6
python clipboard qt4 pyqt pyqt4
source share
2 answers

OK, the clipboard is definitely not visible. QT simply stores some sort of text pointer on the clipboard, not just the text. Gordon Tyler pointed me to this discussion: http://old.nabble.com/Re:-Searching-for-a-very-small-scprit-using-CLIPBOARD-p23246491.html , which explains what is happening. I quote the code and the corresponding part of the explanation.

Run this code when exiting the application (for example, in the 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 is that by default copying something to the clipboard only copies the link / pointer to the source expression. Then, when another application wants to paste data from the clipboard, it requests data from the source application. Calling OleFlushClipboard forces Windows to copy real data to the clipboard instead of the link. Although this causes a delay when copying images, it should not have any noticeable effect with strings.

The above code is quite cross-platform and does no harm to the Linux platform.

+5
source share

I came across this question when I ran into a similar problem in GNU / Linux and found the answer on the site referenced by bialix (the address of which has changed, but can still be reached via web search). To quote the most significant part:

Remember that on Linux, if you do not have something like klipper, when the application that sets the clipboard exits, the clipboard is empty.

So here it is. This is a system thing for GNU / Linux. Here's a more detailed explanation from the ubuntu wiki :

Why is this happening?

The problem arises because Xorg has a conservative approach to copying. It only copies the link to the source data when the user makes a selection or copy. It does not go and does not extract the actual data from the source program until the user asks to insert. This eliminates unnecessary data transfer in this way, due to the lack of a way to get data from a closed program that has not yet saved the clipboard.

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

+1
source share

All Articles