[PyQt] Searching for a very small scprit using CLIPBOARD

Aron Bierbaum aronbierbaum at gmail.com
Sun Apr 26 21:43:41 BST 2009

We ran into a similar situation where we wanted to ensure that a
QPixmap that is copied into the clipboard stays after the application
exits. Although as far as I know this should happen automatically when
your Qt application exists, we had to do this use the following code
manually before exiting our application.

   # Send an event to the clipboard so that it copies the data over to the
   # clipboard instead of keeping a pointer to the data internally. This should
   # happen automatically on exit, but it appears that the mime type convertion
   # methods for Qt -> windows formats get unregistered before this event gets
   # processed. This caused a bug where we couldn't paste data after closing
   # because there was not a conversion method. The magic function on
Windows that
   # needs to be called to do this copy is OleFlushClipboard.
   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 [1] 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.


[1] http://msdn.microsoft.com/en-us/library/ms679707(VS.85).aspx

On Sun, Apr 26, 2009 at 2:34 AM, projetmbc <projetmbc at club-internet.fr> wrote:
> sys.exit(app.exec_()) doesn't work. If the clipboard is empty when the
> program exits, I will do without PyQt so as to have a "universal" clipboard.
> Thanks a lot because now I knows how to use clipboard in a "real" program..
> Christophe.
> Demetrius Cassidy a écrit :
>> The clipboard will empty when your program exits. Afaik this is normal
>> behavior with Qt and clipboard access.
>> I know if I use wxWindows the text in the clipboard stays even if I close
>> my
>> app, however I am not sure if it's possible to do this in Qt.
>> And use sys.exit(app.exec_()) instead.
>> projetmbc wrote:
>>> I've tried the following code but the application never stops because of
>>> app.exec_(), and when I close the application, the clipboard is "empty".
>>> =========================
>>> #!/usr/bin/env python
>>> #coding=utf-8
>>> import sys
>>> from PyQt4 import QtGui
>>> app = QtGui.QApplication(sys.argv)
>>> clipboard = app.clipboard()
>>> clipboard.setText('texte')
>>> app.exec_()
>>> =========================
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

More information about the PyQt mailing list