[PyQt] Searching for a very small scprit using CLIPBOARD

projetmbc projetmbc at club-internet.fr
Mon Apr 27 17:42:27 BST 2009

Thanks a lot, your solution works fine.  :-)  Here is the complete 
minimal code :

#!/usr/bin/env python
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
clipboard = app.clipboard()
clipboard.setText('The text that must be copied...')
event = QtCore.QEvent(QtCore.QEvent.Clipboard)
app.sendEvent(clipboard, event)

Aron Bierbaum a écrit :
> 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.

More information about the PyQt mailing list