PDA

View Full Version : How to save the image in clipboard in variable and restore it to clipboard later?



qtcentre2019
19th January 2020, 11:12
I connect a slot to the dataChanged signal of QClipboard to store the image in the clipboard to a variable mimedata(reference (https://stackoverflow.com/questions/13762140/proper-way-to-copy-a-qmimedata-object)):


void MyWin::clipboardDataChanged()
{
const QMimeData * m=QApplication::clipboard()->mimeData();
mimedata = new QMimeData();

foreach(QString format, m->formats())
{
QByteArray data = m->data(format);
if(format.startsWith("application/x-qt"))
{
int index1 = format.indexOf('"') + 1;
int index2 = format.indexOf('"', index1);
format = format.mid(index1, index2 - index1);
}
mimedata->setData(format, data);
}
}


And restore mimedata to clipboard as follows:



void MyWin::onrestore()
{
QApplication::clipboard()->setMimeData(mimedata);
}


However, the data set to the clipboard seems corrupted. If I paste from the clipboard to Paint, it says "The information on the Clipboard can't be inserted into Paint." I printed the format of the data in the clipboard, i.e., "application/x-qt-image". So I think it is not a format that is supported by other applications. Is this a bug of Qt?

d_stranz
19th January 2020, 17:28
If you want inter-operability with non-Qt apps, like MS Paint, then you need to convert your image into one of the "standard" MIME types, like image/jpg or image/bmp. There is a complete list here (https://www.sitepoint.com/mime-types-complete-list/). This will probably require saving your QImage into one of these image types before pasting it.

qtcentre2019
20th January 2020, 08:10
If you want inter-operability with non-Qt apps, like MS Paint, then you need to convert your image into one of the "standard" MIME types, like image/jpg or image/bmp. There is a complete list here (https://www.sitepoint.com/mime-types-complete-list/). This will probably require saving your QImage into one of these image types before pasting it.

When I press the Print-Screen key to take a screenshot, the only format I got is "application/x-qt-image". I do not know what the standard mime is for the image in the clipboard, how can I convert to a standard mime type?