Using the Clipboard

PyQt provides clipboard support for text in QTextEdit, QLineEdit, QTableWidget, and the other widgets where textual data can be edited. PyQt's clipboard and drag-and-drop systems use data in MIME (Multipurpose Internet Mail Extensions) format, a format that can be used to store any arbitrary data.

Occasionally, it is convenient to pass data to the clipboard or retrieve data from the clipboard directly in code. PyQt makes this easy. The QApplication class provides a static method that returns a QClipboard object, and we can set or get text, images, or other data to or from the clipboard through this object.

The clipboard holds only one object at a time, so if we set, say, a string, and then we set an image, only the image will be available because the string will be deleted when we set the image.

Here is how we set text on the clipboard:

clipboard = QApplication.clipboard() clipboard.setText("I've been clipped!")

The text is set as plain text; we will see how to handle HTML shortly.

clipboard = QApplication.clipboard() clipboard.setPixmap(QPixmap(os.path.join(

os.path.dirname(_file_), "images/gvim.png")))

Image data can be set on the clipboard using setImage() for QImages, or set-Pixmap() for pixmaps, as we have done here. Both QImage and QPixmap can handle a wide variety of standard image formats.

Retrieving data from the clipboard is just as easy:

clipboard = QApplication.clipboard() self.textLabel.setText(clipboard.text())

If the clipboard has no text—for example, if it has an image, or some custom data type—QClipboard.text() will return an empty string.

clipboard = QApplication.clipboard() self.imageLabel.setPixmap(clipboard.pixmap())

If the clipboard has no image—for example, if it has text, or some custom data type—QClipboard.pixmap() will return a null image.

In addition to handling plain text and images we can handle some other kinds of data. For example, here is how we would copy HTML text to the clipboard:

mimeData = QMimeData() mimeData.setHtml(

"<b>Bold and <font color=red>Red</font></b>") clipboard = QApplication.clipboard() clipboard.setMimeData(mimeData)

If we want to retrieve HTML text, including HTML wrapped in a QMimeData object, we can use QClipboard.text("html"). This will return an empty string if there is no text, or if the text is not in HTML format—for example, if it is plain text. Here is the generic way to retrieve data that has been wrapped in a QMimeData object:

clipboard = QApplication.clipboard() mimeData = clipboard.mimeData()

if mimeData.hasHtml():


In some situations we want to set and get our own custom data formats to and from the clipboard. We can do this using the QMimeData class, as we will see in the next section.

Data set on or retrieved from the clipboard usually works on the operating system's global clipboard. In addition, by specifying the clipboard mode, it is possible to use the selection clipboard (an additional clipboard that exists on Linux and other systems that use the X Window System), or the find pasteboard used on Mac OS X.

+6 0


  • vanessa
    How to read clipboard with pyqt?
    6 years ago
  • hanna-mari
    How to retrieve data from lineedit in qtablewidget using pyqt?
    5 years ago

Post a comment