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. How to save the image in clipboard in variable and restore it to clipboard later?
Forum Updated to NodeBB v4.3 + New Features

How to save the image in clipboard in variable and restore it to clipboard later?

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 7 Posters 4.5k 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #12

    Actually, it's much easier than I was thinking, you can load images in the clipboard just by using QApplication::clipboard()->setPixmap or QApplication::clipboard()->setImage.
    For example:

    #include <QApplication>
    #include <QPushButton>
    #include <QClipboard>
    #include <QPixmap>
    int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
        QPushButton quitButton(QStringLiteral("SaveImage"));
        QObject::connect(&quitButton,&QPushButton::clicked,[&quitButton](){
            QApplication::clipboard()->setPixmap(quitButton.grab());
        });
        quitButton.show();
        return app.exec();
    }
    

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yetanotherqtfan
      wrote on last edited by yetanotherqtfan
      #13

      Yes, it has no problem to set a QImage or QPixmap to clipboard. But it has problem getting the image from clipboard and setting it back to clipboard. And I want to restore every component in the clipboard not just image, which means I must copy the mimedata.

      VRoninV 1 Reply Last reply
      0
      • Y yetanotherqtfan

        Yes, it has no problem to set a QImage or QPixmap to clipboard. But it has problem getting the image from clipboard and setting it back to clipboard. And I want to restore every component in the clipboard not just image, which means I must copy the mimedata.

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #14

        @yetanotherqtfan said in How to save the image in clipboard in variable and restore it to clipboard later?:

        And I want to restore every component in the clipboard not just image

        "application/x-qt-image" is the only format when I press the Print-Screen key to take a screenshot.

        I don't think I follow

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        Christian EhrlicherC 1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yetanotherqtfan
          wrote on last edited by
          #15

          For the print-screen case, there is only one format "application/x-qt-image". But in most cases,e.g., copying images and text in a word document, there would be more components in the clipboard. In the print-screen case, you can copy the clipboard image to a QImage object then set it to clipboard later. But for other cases, you may get a null image. Even you get a valid QImage, setting it back to clipboard will destroy other components in it. So the general method would be copying the mimedata object, and this would fail even for the print-screen case.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #16

            Something like this?

            bool skipOne = false;
                QObject::connect(app.clipboard(),&QClipboard::dataChanged,[&skipOne](){
                    if(skipOne){
                        skipOne=false;
                        return;
                    }
                    const QMimeData* mime = QApplication::clipboard()->mimeData();
                    const QStringList formats = mime->formats();
                    const int imageIdx = formats.indexOf(QLatin1String("application/x-qt-image"));
                    if(imageIdx<0)
                        return;
                    const QImage image = qvariant_cast<QImage>(mime->imageData());
                    if(image.isNull())
                        return;
                    QMimeData* adjMime = new QMimeData;
                    adjMime->setImageData(image);
                    for(int i=0;i<formats.size();++i){
                        if(i==imageIdx)
                            continue;
                        adjMime->setData(formats.at(i),mime->data(formats.at(i)));
                    }
                    skipOne=true;
                    QApplication::clipboard()->setMimeData(adjMime);
                });
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • Y Offline
              Y Offline
              yetanotherqtfan
              wrote on last edited by
              #17

              @VRonin said in How to save the image in clipboard in variable and restore it to clipboard later?:

              dat

              I will try your latest code. But all my attempts in setting the QMimeData failed.

              1 Reply Last reply
              0
              • VRoninV VRonin

                @yetanotherqtfan said in How to save the image in clipboard in variable and restore it to clipboard later?:

                And I want to restore every component in the clipboard not just image

                "application/x-qt-image" is the only format when I press the Print-Screen key to take a screenshot.

                I don't think I follow

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #18

                @VRonin said in How to save the image in clipboard in variable and restore it to clipboard later?:

                "application/x-qt-image" is the only format when I press the Print-Screen key to take a screenshot.

                I don't think I follow

                The problem is that @yetanotherqtfan does not understand that this is the one and only format you will get from Qt since when the data is retrieved from the native clipboard and there is image data it will be converted to this internal mime-format. And the other way round so there is absolutely no need to replace the mime-type (it's even counter-productive - see https://code.woboq.org/qt5/qtbase/src/plugins/platforms/windows/qwindowsmime.cpp.html#1140 )

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                3
                • D Offline
                  D Offline
                  derekleesoft
                  wrote on last edited by
                  #19

                  @Christian-Ehrlicher I have one question. I tried to save the transparent image to clipboard but it's not working on windows but on ubuntu. I am not sure why. When I used setImage or setPixmap, the image was always opaque even it has transparency. How can I fix this?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SilentSight
                    wrote on last edited by
                    #20

                    I'm having a similar issue to the original poster (Qt5 for Python), and I think I can clarify what they're trying to say.

                    So when copying from the Clipboard, many programs add additional MimeType metadata. For example, GIMP's XCF when copying images will include XCF metadata, plus several different variations of the image's binary data.

                    For example:

                    ['TIMESTAMP', 'TARGETS', 'MULTIPLE', 'SAVE_TARGETS', 'image/x-xcf', 'image/png', 'image/bmp', 'image/x-bmp', 'image/x-MS-bmp', 'image/x-icon', 'image/x-ico', 'image/x-win-bitmap', 'image/vnd.microsoft.icon', 'application/ico', 'image/ico', 'image/icon', 'text/ico', 'image/tiff', 'image/jpeg', 'application/x-qt-image']
                    

                    However, Qt does not allow one to faithfully recreate said mimeType metadata. If one tries to use 'setImage', for example, Qt forces their proprietary format:

                    application/x-qt-image
                    

                    This is not a valid image mimetype for image data (which must start with "image/" and should be a common recognised format, E.G. png), which means applications like GIMP will not recognise the image data as valid.

                    There's little point using Qt-specific tags in a global clipboard meant to be used/referenced by external programs.

                    Trying to use the Qt5 ecosystem, it feels very much like one is coerced into using 'Q-types' even if they might not be suitable nor desirable for third party application specific purposes.

                    Even if one tries to set a custom mimetype via 'setMimeData', and specifies explicitly what the raw binary data is (E.G. 'image/png'), third party applications still do not recognise it as valid PNG data - even if it is a verbatim cloned copy of the raw image/png data straight from Qt!

                    Even if this was to work, any subsequent calls to 'setMimeData' overwrites the last set format (so if I try to also set 'image/jpeg' trying to recreate XCF's metadata, it will just replace 'image/png'). There's presently no way to set multiple types of mimetype metadata at once.

                    C 1 Reply Last reply
                    0
                    • S SilentSight

                      I'm having a similar issue to the original poster (Qt5 for Python), and I think I can clarify what they're trying to say.

                      So when copying from the Clipboard, many programs add additional MimeType metadata. For example, GIMP's XCF when copying images will include XCF metadata, plus several different variations of the image's binary data.

                      For example:

                      ['TIMESTAMP', 'TARGETS', 'MULTIPLE', 'SAVE_TARGETS', 'image/x-xcf', 'image/png', 'image/bmp', 'image/x-bmp', 'image/x-MS-bmp', 'image/x-icon', 'image/x-ico', 'image/x-win-bitmap', 'image/vnd.microsoft.icon', 'application/ico', 'image/ico', 'image/icon', 'text/ico', 'image/tiff', 'image/jpeg', 'application/x-qt-image']
                      

                      However, Qt does not allow one to faithfully recreate said mimeType metadata. If one tries to use 'setImage', for example, Qt forces their proprietary format:

                      application/x-qt-image
                      

                      This is not a valid image mimetype for image data (which must start with "image/" and should be a common recognised format, E.G. png), which means applications like GIMP will not recognise the image data as valid.

                      There's little point using Qt-specific tags in a global clipboard meant to be used/referenced by external programs.

                      Trying to use the Qt5 ecosystem, it feels very much like one is coerced into using 'Q-types' even if they might not be suitable nor desirable for third party application specific purposes.

                      Even if one tries to set a custom mimetype via 'setMimeData', and specifies explicitly what the raw binary data is (E.G. 'image/png'), third party applications still do not recognise it as valid PNG data - even if it is a verbatim cloned copy of the raw image/png data straight from Qt!

                      Even if this was to work, any subsequent calls to 'setMimeData' overwrites the last set format (so if I try to also set 'image/jpeg' trying to recreate XCF's metadata, it will just replace 'image/png'). There's presently no way to set multiple types of mimetype metadata at once.

                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by
                      #21

                      @SilentSight said in How to save the image in clipboard in variable and restore it to clipboard later?:

                      However, Qt does not allow one to faithfully recreate said mimeType metadata.

                      My test program begs to differ:

                      #include <QApplication>
                      #include <QCryptographicHash>
                      #include <QClipboard>
                      #include <QMimeData>
                      #include <QDebug>
                      
                      void dumpCB(const QMimeData *m) {
                          foreach (const QString &format, m->formats() ) {
                              const QByteArray data = m->data(format);
                              qDebug() << format << data.size() << "bytes" << QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex();
                          }
                      }
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                      
                          QClipboard *clipboard = QGuiApplication::clipboard();
                      
                          // Dump current clipboard content
                          qDebug() << "Starting clipboard content:";
                          dumpCB(clipboard->mimeData());
                      
                          // Save current clipboard content
                          QMimeData *mdSave = new QMimeData();
                          const QMimeData *m = clipboard->mimeData();
                          foreach (const QString &format, m->formats() ) {
                              mdSave->setData(format, m->data(format));
                          }
                      
                          qDebug() << "Saved clipboard content:";
                          dumpCB(mdSave);
                      
                          // Clear the clipboard
                          clipboard->clear();
                      
                          // Dump current clipboard content
                          qDebug() << "Cleared clipboard content:";
                          dumpCB(clipboard->mimeData());
                      
                          // Restore the clipboard
                          clipboard->setMimeData(mdSave);
                      
                          // Dump current clipboard content
                          qDebug() << "Restored clipboard content:";
                          dumpCB(clipboard->mimeData());
                      
                          return 0;
                      }
                      

                      Output when starting clipboard contains small image copied from Firefox:

                      Starting clipboard content:
                      "TIMESTAMP" 4 bytes "f11b876fb69d51c23c88f4dc549d7b54"
                      "TARGETS" 84 bytes "97eb07e16cdb61c9a15427e618004c07"
                      "MULTIPLE" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "SAVE_TARGETS" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "text/html" 343 bytes "430ff9e5ce38b44605b33f421df1d84a"
                      "text/_moz_htmlinfo" 6 bytes "00f8c2ad49f4f1df4476b93a9470d69d"
                      "text/_moz_htmlcontext" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "image/png" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      "image/jpeg" 3578 bytes "c80bcacb87f3c25dae2aa357050ed508"
                      "image/bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-MS-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/x-ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/x-win-bitmap" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/vnd.microsoft.icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "application/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "text/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/tiff" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      
                      Saved clipboard content:
                      "TIMESTAMP" 4 bytes "f11b876fb69d51c23c88f4dc549d7b54"
                      "TARGETS" 84 bytes "97eb07e16cdb61c9a15427e618004c07"
                      "MULTIPLE" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "SAVE_TARGETS" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "text/html" 343 bytes "430ff9e5ce38b44605b33f421df1d84a"
                      "text/_moz_htmlinfo" 6 bytes "00f8c2ad49f4f1df4476b93a9470d69d"
                      "text/_moz_htmlcontext" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "image/png" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      "image/jpeg" 3578 bytes "c80bcacb87f3c25dae2aa357050ed508"
                      "image/bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-MS-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/x-ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/x-win-bitmap" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/vnd.microsoft.icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "application/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "text/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/tiff" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      
                      Cleared clipboard content:
                      
                      Restored clipboard content:
                      "TIMESTAMP" 4 bytes "f11b876fb69d51c23c88f4dc549d7b54"
                      "TARGETS" 84 bytes "97eb07e16cdb61c9a15427e618004c07"
                      "MULTIPLE" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "SAVE_TARGETS" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "text/html" 343 bytes "430ff9e5ce38b44605b33f421df1d84a"
                      "text/_moz_htmlinfo" 6 bytes "00f8c2ad49f4f1df4476b93a9470d69d"
                      "text/_moz_htmlcontext" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "image/png" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      "image/jpeg" 3578 bytes "c80bcacb87f3c25dae2aa357050ed508"
                      "image/bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-MS-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                      "image/x-icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/x-ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/x-win-bitmap" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/vnd.microsoft.icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "application/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "text/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                      "image/tiff" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                      "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      

                      You will note that Qt presents an "image/png" format and an "application/x-qt-image" format (even though the source is not a Qt executable) and that they are identical data:

                      "image/png"              27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                      

                      This is virtual mapping and I assume that internally they are the same buffer. You will see that Firefox (not Qt) has similar shared arrangement for other "non-standard" types e.g., "image/bmp", "image/x-bmp", and "image/x-MS-bmp", and many more for various icon forms.

                      The save/restore approach in the example is possibly not efficient because it may cause several copies of identical data where the source was shared. Without inspecting the Qt source I cannot say if there is a better way.

                      There's presently no way to set multiple types of mimetype metadata at once.

                      Sure there is. Put several different types into a QMimeData and call QClipboard::setMimeData().

                      I do, however, see that the restored clipboard is not recognised by some other programs. Gimp does not but Libreoffice Draw does. I do not have the time to debug that.

                      O 1 Reply Last reply
                      0
                      • C ChrisW67

                        @SilentSight said in How to save the image in clipboard in variable and restore it to clipboard later?:

                        However, Qt does not allow one to faithfully recreate said mimeType metadata.

                        My test program begs to differ:

                        #include <QApplication>
                        #include <QCryptographicHash>
                        #include <QClipboard>
                        #include <QMimeData>
                        #include <QDebug>
                        
                        void dumpCB(const QMimeData *m) {
                            foreach (const QString &format, m->formats() ) {
                                const QByteArray data = m->data(format);
                                qDebug() << format << data.size() << "bytes" << QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex();
                            }
                        }
                        
                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                        
                            QClipboard *clipboard = QGuiApplication::clipboard();
                        
                            // Dump current clipboard content
                            qDebug() << "Starting clipboard content:";
                            dumpCB(clipboard->mimeData());
                        
                            // Save current clipboard content
                            QMimeData *mdSave = new QMimeData();
                            const QMimeData *m = clipboard->mimeData();
                            foreach (const QString &format, m->formats() ) {
                                mdSave->setData(format, m->data(format));
                            }
                        
                            qDebug() << "Saved clipboard content:";
                            dumpCB(mdSave);
                        
                            // Clear the clipboard
                            clipboard->clear();
                        
                            // Dump current clipboard content
                            qDebug() << "Cleared clipboard content:";
                            dumpCB(clipboard->mimeData());
                        
                            // Restore the clipboard
                            clipboard->setMimeData(mdSave);
                        
                            // Dump current clipboard content
                            qDebug() << "Restored clipboard content:";
                            dumpCB(clipboard->mimeData());
                        
                            return 0;
                        }
                        

                        Output when starting clipboard contains small image copied from Firefox:

                        Starting clipboard content:
                        "TIMESTAMP" 4 bytes "f11b876fb69d51c23c88f4dc549d7b54"
                        "TARGETS" 84 bytes "97eb07e16cdb61c9a15427e618004c07"
                        "MULTIPLE" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "SAVE_TARGETS" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "text/html" 343 bytes "430ff9e5ce38b44605b33f421df1d84a"
                        "text/_moz_htmlinfo" 6 bytes "00f8c2ad49f4f1df4476b93a9470d69d"
                        "text/_moz_htmlcontext" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "image/png" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        "image/jpeg" 3578 bytes "c80bcacb87f3c25dae2aa357050ed508"
                        "image/bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-MS-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/x-ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/x-win-bitmap" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/vnd.microsoft.icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "application/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "text/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/tiff" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        
                        Saved clipboard content:
                        "TIMESTAMP" 4 bytes "f11b876fb69d51c23c88f4dc549d7b54"
                        "TARGETS" 84 bytes "97eb07e16cdb61c9a15427e618004c07"
                        "MULTIPLE" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "SAVE_TARGETS" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "text/html" 343 bytes "430ff9e5ce38b44605b33f421df1d84a"
                        "text/_moz_htmlinfo" 6 bytes "00f8c2ad49f4f1df4476b93a9470d69d"
                        "text/_moz_htmlcontext" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "image/png" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        "image/jpeg" 3578 bytes "c80bcacb87f3c25dae2aa357050ed508"
                        "image/bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-MS-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/x-ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/x-win-bitmap" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/vnd.microsoft.icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "application/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "text/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/tiff" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        
                        Cleared clipboard content:
                        
                        Restored clipboard content:
                        "TIMESTAMP" 4 bytes "f11b876fb69d51c23c88f4dc549d7b54"
                        "TARGETS" 84 bytes "97eb07e16cdb61c9a15427e618004c07"
                        "MULTIPLE" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "SAVE_TARGETS" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "text/html" 343 bytes "430ff9e5ce38b44605b33f421df1d84a"
                        "text/_moz_htmlinfo" 6 bytes "00f8c2ad49f4f1df4476b93a9470d69d"
                        "text/_moz_htmlcontext" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "image/png" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        "image/jpeg" 3578 bytes "c80bcacb87f3c25dae2aa357050ed508"
                        "image/bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-MS-bmp" 49206 bytes "d5a381d0c4f2d730430e88ce8c58920d"
                        "image/x-icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/x-ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/x-win-bitmap" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/vnd.microsoft.icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "application/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/icon" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "text/ico" 67646 bytes "b6bf4a8d2474fc089606659729f96bb0"
                        "image/tiff" 0 bytes "d41d8cd98f00b204e9800998ecf8427e"
                        "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        

                        You will note that Qt presents an "image/png" format and an "application/x-qt-image" format (even though the source is not a Qt executable) and that they are identical data:

                        "image/png"              27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        "application/x-qt-image" 27304 bytes "7eed20e7396dc51d57ddd421168733b9"
                        

                        This is virtual mapping and I assume that internally they are the same buffer. You will see that Firefox (not Qt) has similar shared arrangement for other "non-standard" types e.g., "image/bmp", "image/x-bmp", and "image/x-MS-bmp", and many more for various icon forms.

                        The save/restore approach in the example is possibly not efficient because it may cause several copies of identical data where the source was shared. Without inspecting the Qt source I cannot say if there is a better way.

                        There's presently no way to set multiple types of mimetype metadata at once.

                        Sure there is. Put several different types into a QMimeData and call QClipboard::setMimeData().

                        I do, however, see that the restored clipboard is not recognised by some other programs. Gimp does not but Libreoffice Draw does. I do not have the time to debug that.

                        O Offline
                        O Offline
                        OtterNonsense
                        wrote last edited by
                        #22

                        @ChrisW67 said in How to save the image in clipboard in variable and restore it to clipboard later?:

                        I do, however, see that the restored clipboard is not recognised by some other programs. Gimp does not but Libreoffice Draw does. I do not have the time to debug that.

                        It looks like GIMP doesn't recognise the "image/png" MIME type - adding the same data under the name "PNG" works.

                        def copy_image_data_to_clipboard(image: QImage):
                            """ Copy QImage to system clipboard """
                            if image and not image.isNull():
                                clipboard = QGuiApplication.clipboard()
                                mime_data = QMimeData()
                                mime_data.setImageData(image)
                                png_byte_array = QByteArray()
                                buffer = QBuffer(png_byte_array)
                                buffer.open(QIODevice.OpenModeFlag.WriteOnly)
                                image.save(buffer, "PNG")
                                buffer.close()
                                mime_data.setData("PNG", png_byte_array)
                                clipboard.setMimeData(mime_data)
                        

                        (I appreciate this is an old post, but it's the most relevant Google search result I found, so thought I would leave this for anyone else with the same problem...)

                        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