[英]Qt - QPixmap to XML and conversely
Is there a way to save an image as "text" and then read it back? 有没有一种方法可以将图像另存为“文本”,然后将其读回? Indeed I'm using Qt and I'm trying to save/read a base64-encoded representation of a pixmap but nothing works as expected. 确实,我正在使用Qt,并且试图保存/读取pixmap的base64编码表示形式,但是没有按预期工作。 Here is my current implementation sprinkled with several comments: 这是我当前的实现,其中充满了一些评论:
/*
* the culprit pixmap
*/
QPixmap pixmap = QPixmap(a_given_jpg_filepath);
/*
* pixmap to array of bytes
*/
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
pixmap.save(&buffer, "jpg"); // writes pixmap into bytes in jpg format
/*
* byte array to text
* this string is saved to xml (among others) and then read back
*/
const QString &str(bytes.toBase64()); // a base64-encoded version of the pixmap
/*
* now trying to construct the pixmap (having it encoded as base64)
*/
const auto wantConversion = true;
if(wantConversion) {
const QByteArray &bytes = QByteArray::fromBase64(str.toLatin1());
QPixmap(bytes); // this pixmap is not the same as the previous one
}
What am I doing wrong? 我究竟做错了什么? Am I on the right route to the solution? 我在正确的解决方案上吗? Any fix/remark is welcome. 任何修复/备注均受欢迎。
You need to call loadFromData()
您需要调用loadFromData()
const QByteArray &bytes = QByteArray::fromBase64(str.toLatin1());
QPixmap fromBase64;
fromBase64.loadFromData(bytes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.