如何将 QPixmap 对象保存到文件?

发布于 2024-08-24 11:42:28 字数 558 浏览 5 评论 0 原文

我在读取 QByteArray 数据并将其写入文件时遇到问题。

我的目标是将 QPixmap 数据保存到 QByteArray 中,并将该 QByteArray 保存到文件中(能够从文件中读回此 QByteArray 并保存到 QPixmap 中)。我想使用 QPixmap 文档中的以下代码:

     QPixmap pixmap(<image path>);  
     QByteArray bytes;
     QBuffer buffer(&bytes);
     buffer.open(QIODevice::WriteOnly);
     pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format

将缓冲区写入文件后,我希望能够检索 QByteArray 并使用 QPixmap::loadFromData() 函数将其加载回 QPixmap 中。

如果需要任何进一步的说明,请告诉我(我也愿意接受其他方法,我只需要能够读取 QPixmap 并将其写入文件!:));

I'm having trouble reading and writing QByteArray data to a file.

My goal is to save QPixmap data into a QByteArray and save that QByteArray to a file (with the ability to read this QByteArray back from the file and into a QPixmap). I want to use following code from the QPixmap documentation:

     QPixmap pixmap(<image path>);  
     QByteArray bytes;
     QBuffer buffer(&bytes);
     buffer.open(QIODevice::WriteOnly);
     pixmap.save(&buffer, "PNG"); // writes pixmap into bytes in PNG format

After writing the buffer to a file, I want to be able to retrieve the QByteArray and load it back into a QPixmap using the QPixmap::loadFromData() function.

Please let me know if any further clarification is needed (I'm open to alternative approaches as well, I just need to be able to read and write the QPixmap to a file! :) );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-08-31 11:42:28

这似乎是一个很长的路要走(但你的评论更好地解释了):

对于写作:

QFile file("yourFile.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");

对于阅读:

QPixmap pixmap;
pixmap.load("yourFile.png");

当你需要一个QIODevice并且想要时,QBuffer非常有用将其保留在内存中,但如果您实际上要写入磁盘,那么这是不必要的中间步骤。

编辑:

要将像素图和其他内容写入单个文件,我建议您使用 QDataStream

对于写入:

QFile file("outfile.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << QString("almost any qt value object")
    << yourQPixMap << yourQList /* << etc. */;

然后,您可以对读取进行类似的操作:

QFile file("infile.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> firstQString >> yourQPixmap >> yourList /* >> etc. */;

您需要确保读入的对象与写出的对象相同。为了避免将来出现兼容性问题,请显式设置 QDataStream 版本。

That seemed like a really long way to go about doing it (but your comment better explains):

For writing:

QFile file("yourFile.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");

For reading:

QPixmap pixmap;
pixmap.load("yourFile.png");

QBuffer is great when you need a QIODevice and want to keep it in memory, but if you're actually going out to disk, then it's an unnecessary middle step.

EDIT:

To write pixmaps, and other things, to a single file I'd recommend that you use QDataStream.

For writing:

QFile file("outfile.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << QString("almost any qt value object")
    << yourQPixMap << yourQList /* << etc. */;

Then, you can do similarly for reading:

QFile file("infile.dat");
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> firstQString >> yourQPixmap >> yourList /* >> etc. */;

You'll need to make sure that you read in the same objects as you wrote them out. In order to save yourself future compatibility headaches, set the QDataStream version explicitly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文