繁体   English   中英

Qt-QPixmap到XML以及相反

[英]Qt - QPixmap to XML and conversely

有没有一种方法可以将图像另存为“文本”,然后将其读回? 确实,我正在使用Qt,并且试图保存/读取pixmap的base64编码表示形式,但是没有按预期工作。 这是我当前的实现,其中充满了一些评论:

/*
 * 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
}

我究竟做错了什么? 我在正确的解决方案上吗? 任何修复/备注均受欢迎。

您需要调用loadFromData()

const QByteArray &bytes = QByteArray::fromBase64(str.toLatin1());
QPixmap fromBase64;
fromBase64.loadFromData(bytes);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM