如何使用fopen访问资源映像?

编程入门 行业动态 更新时间:2024-10-25 20:29:18
本文介绍了如何使用fopen访问资源映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Qt中将fopen(filename.c_str(), "rb");用于图像资源.

I use fopen(filename.c_str(), "rb"); in Qt with images resources.

但是fopen需要完整的路径.如果我的文件名是:/images/img.png,则不起作用!

But fopen wants a full path. It doesn't work if my filename is :/images/img.png!

推荐答案

fopen不属于Qt. Qt的资源系统仅适用于Qt类.请参阅在应用程序中使用资源的Qt文档了解详情.

fopen is not part of Qt. Qt's resource system only works with Qt classes. See the Qt documentation of Using Resources in the Application for details.

适用于资源的Qt类首先检查给定的文件名是否以资源标记(:)开头,如果是,则它们在内部查找资源(没有打开物理文件!),否则,它们将打开物理文件.请注意,资源在编译期间(使用Qt资源编译器)包含在应用程序的源代码中(作为字节的简单数组),并在Qt内部使用特殊的资源读取器进行读取,可以将资源文件视为将会是一个物理文件,但仍然不是.

The Qt classes which work for resources first check if the given filename starts with the resource marker (:) and if so, they look up the resource internally (no physical file is opened!), otherwise, they open the physical file. Note that resources are included in the source code of your application (as a simple array of bytes) during compile time (using the Qt resource compiler) and read using a special resource reader in Qt internally, which can treat the resource file as it would be a physical file, but it still isn't.

如果将资源文件路径(以:开头)传递到fopen,会发生什么情况? fopen尝试读取名为:的物理目录,并在该目录中搜索您提供的子路径.当然,由于该文件是可执行文件的一部分,因此硬盘驱动器上不存在此类目录. (而且,请勿尝试通过使用fopen打开可执行文件并搜索资源来从可执行文件中手动读取资源文件!)

What happens if you pass a resource file path (starting with :) to fopen? fopen tries to read a physical directory called : and searches in this directory for the subpath you provided. Of course, no such directory exists on your harddrive, since the file is part of your executable file. (And please don't try to read the resource file manually from within the executable by opening the executable using fopen and seeking to the resource!)

在您的具体情况下:您是否正在尝试使用外部图像读取器读取图像? Qt拥有自己的PNG阅读器.只需调用 QImage(QString filename) 构造函数即可.

In your concrete situation: Are you trying to read the image using an external image reader? Qt has its own PNG reader. Just call the QImage(QString filename) constructor.

更多推荐

如何使用fopen访问资源映像?

本文发布于:2023-11-13 06:41:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:映像   如何使用   资源   fopen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!