akshay
akshay

Reputation: 93

How to get clipboard image from Qclipboard mimeData in c++?

So here is the deal, I am trying to get clipboard image (any format), and the method I am trying either returns NULL or return text formatted path of image like this

enter image description here

I've googled it and I've asked on reddit as well, but I couldn't figure out what's going on.

So there are the methods I've tried

(1)

const QClipboard *cb = QApplication::clipboard();
const QMimeData *md = cb->mimeData();
QListWidgetItem *item = new QListWidgetItem();
QLabel *label = new QLabel();
if (md->hasImage())
{

    label->setPixmap(cb->pixmap());
    ui.listWidget->setIconSize(QSize(100, 200));
    ui.listWidget->addItem(item);
    ui.listWidget->setItemWidget(item, label);

}
else if (md->hasText())
{
    ui.listWidget->addItem(cb->text());

} 

(2)

label->setPixmap(cb->image());

(3)

HBITMAP MemoryManager::ImageFomClipboard(HWND hWnd)
{
    if (!OpenClipboard(hWnd))
        return NULL;

    HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
    CloseClipboard();
    return hBitmap;
}

std::string MemoryManager::get_image(std::string number)
{
    HWND hWnd = GetDesktopWindow();
    HBITMAP hBitmap = ImageFomClipboard(hWnd);
    if (hBitmap != NULL)
    {
        CImage image;
        image.Attach(hBitmap);
        image.Save(_T("C:/temp/asx.bmp"), Gdiplus::ImageFormatBMP);
    }
    return "C:/temp/asx.bmp";
}

(1) st method was returning the path of image

(2) nd method returned null

(3) rd method returned only print screen images. Don't know why, could be because of .bmp format.

I am completely clueless now how do get this issue sorted, please help me out.

Upvotes: 4

Views: 1460

Answers (1)

akshay
akshay

Reputation: 93

All thanks to @Xplatforms i got this solution,

QImage img(cb->image()); label->setPixmap(QPixmap::fromImage(img));
else if(md->formats().contains(QStringLiteral("text/uri-list"))) { QImage img(QUrl(cb->text()).toLocalFile());
     label->setPixmap(QPixmap::fromImage(img));

Thanks again @Xplatforms :) you rock.

Upvotes: 2

Related Questions