Copying image to clipboard fails
Solved
General and Desktop
-
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
-
Hi,
What version of Qt are you using ? On what platform ?
-
-
Using the same code ?