如何使用fopen访问资源映像? [英] How to access resource image with fopen?

查看:88
本文介绍了如何使用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访问资源映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆