Getting a text or a image from the Linux clipboard and saving it to a file using QT

#include <QApplication>
#include <QClipboard>
#include <QMimeData>
#include <stdio.h>
#include "window.h"

int main(int argc, char *argv[])
    QApplication app(argc, argv);

    QClipboard *clipboard = QApplication::clipboard();
    const QMimeData *mimeData = clipboard->mimeData();
    QPixmap pixmap;

    if (clipboard == 0) {
        return 1;

    if (mimeData->hasImage()) {
        printf("Found an image. Saving to /tmp/test.ppm\r\n");
        pixmap = qvariant_cast<QPixmap>(mimeData->imageData());"/tmp/test.ppm");

    } else if (mimeData->hasText()) {
        printf("Found a text. Writing to stdout\r\n");
        printf("Text: %s\r\n", clipboard->text().toStdString().c_str());
    } else
        printf("Unknown data\r\n");

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s