Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Empty image in QClipboard



  • Hello
    I am trying to get an image from the clipboard using this code. I click on the "copy image" button in Google Chrome and then press "Shift + Insert" in my application, I get to the first branch of the if-statement and see "QRect (0,0 0x0)" in the console. Only after I press "Shift + Insert" several times - the image will appear - QRect(0,0 1280x720).

    uname -a Linux max-pc 5.4.0-70-generic #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

    What am i doing wrong? regards, max

    void CanvasScene::keyPressEvent(QKeyEvent *event)
    {
        switch (event->key()) {
        case (Qt::Key_Insert):
            if (event->modifiers() & Qt::ShiftModifier) {
                pasteFromClipboard();
            }
        break;
        default:
            QGraphicsScene::keyPressEvent(event);
        break;
        }
    }
    
    void CanvasScene::pasteFromClipboard()
    {
        const QClipboard *clipboard = QApplication::clipboard();
        const QMimeData *mimedata = clipboard->mimeData();
    
        if (mimedata->hasImage()) {
            LOG_DEBUG(logger, "IMAGE");
            QImage image = clipboard->image(QClipboard::Clipboard);//qvariant_cast<QImage>(mimedata->imageData());
            qDebug()<<image.rect();
        } else if (mimedata->hasText()) {
            LOG_DEBUG(logger, "TEXT: ", mimedata->text().toStdString());
        } else {
            LOG_DEBUG(logger, "CANNOT DISPLAY DATA");
        }
    }
    

  • Lifetime Qt Champion

    Hi,

    Did you check the notes about X11 clipboard in the QClipBoard documentation ? There might be some thing there applying to your case.




Log in to reply