Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Copying image to clipboard fails
QtWS25 Last Chance

Copying image to clipboard fails

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aran
    wrote on last edited by
    #1

    Hi everybody,
    For my application, I try to copy an image to the clipboard. The image is loaded successfully to the QImage, but calling setImage() on the clipboard does simply nothing. Here is a simple compileable example, and you can get a tar containing all of these files here. Does anybody know what I am doing wrong?

    // clipboard.pro
    
    QT += core gui widgets
    
    TARGET = Clipboard
    TEMPLATE = app
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    RESOURCES += image.qrc
    
    // main.cpp
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[]) {
    
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    // mainwindow.h
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QHBoxLayout>
    #include <QPushButton>
    #include <QApplication>
    #include <QClipboard>
    
    class MainWindow : public QMainWindow {
    
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void copy();
    };
    
    #endif // MAINWINDOW_H
    
    // mainwindow.cpp
    
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    
        QWidget *center = new QWidget;
        this->setCentralWidget(center);
        QHBoxLayout *lay = new QHBoxLayout;
        center->setLayout(lay);
        QPushButton *but = new QPushButton("Copy");
        lay->addWidget(but);
        connect(but, &QPushButton::clicked, this, &MainWindow::copy);
    
    }
    
    void MainWindow::copy() {
    
        QImage img("image.jpg");
    
        QApplication::clipboard()->setImage(img, QClipboard::Clipboard);
        QApplication::clipboard()->setImage(img, QClipboard::Selection);
    
    }
    
    MainWindow::~MainWindow() { }
    

    image.jpg
    image

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What version of Qt are you using ? On what platform ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aran
        wrote on last edited by
        #3

        I'm using Qt 5.8.0, running Arch Linux 64Bit

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kenchan
          wrote on last edited by
          #4

          From the QClipboard docs, why not try this...
          QMimeData *data = new QMimeData;
          data->setImageData(image);
          clipboard->setMimeData(data, mode);

          or use a clipboard->setPixmap(...) make the pixmap from your image. Pixmaps always work like this for me.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aran
            wrote on last edited by
            #5

            Using QPixmap strangely worked... Thanks!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Using the same code ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved