如何在没有 Qt 项目的情况下在 Visual Studio 中使用 Qt 资源文件? [英] How to use Qt resource files in Visual Studio without a Qt project?

查看:42
本文介绍了如何在没有 Qt 项目的情况下在 Visual Studio 中使用 Qt 资源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的程序中使用 Qt 的资源系统.Visual Studio 的资源系统在加载 .png、.bmp、.jpeg 等非基于文本的文件时让我头疼.我读过一些关于 Qt 的文章,看起来很有趣.我想使用它的资源系统.我已经下载了 Qt Visual Studio Tool 扩展.我还制作了一个资源文件,其中包含我想嵌入到可执行文件(不是 Qt 的可执行文件)中的文件.我做了一个 Visual Studio 项目,而不是 Qt 项目.我安装扩展的唯一原因是将资源嵌入到我的可执行文件中并访问它们.另外,如果可能的话,我想在我的非 Qt 项目中包含 Qt 的头文件,这样我就可以轻松地注册和阅读这些资源.所以,简而言之,这就是我想要做的:

I want to use Qt's resource system for my program. Visual Studio's resource system is giving me headaches while loading non text based files like .png, .bmp, .jpeg, etc. I have read a bit about Qt and it seems very interesting. I would like to use its resource system. I have downloaded the Qt Visual Studio Tool extension. I have also made a resource file containing the files that I would like to embed in the executable (NOT Qt's executable). I have made a Visual Studio Project, NOT a Qt Project. The only reason I installed the extension is to embed the resources in my executable and access them. Also, if possible I would like to include Qt's headers in my non-Qt Project, so I can easily register and read those resources. So, in a nutshell, this is what I want to do:

  1. 使用 Qt 将我的资源嵌入到可执行文件中.
  2. 在我的程序中访问这些文件.

像这样:

#include <Qtheaderfiles>

struct FileData {
    unsigned char* bytes;
    unsigned int size;
};

FileData loadFromResource(const std::string& res) {
    QtFile file(res);

    //get all the data from the file and return it in bytes.
    return FileData { fileBytes, fileSize };
}

即使res"目录中没有实际文件,我也希望上述代码能够编译,因为所有资源都已嵌入到可执行文件中.

I expect the above code to compile even when there is no actual file in the directory "res", because all of the resources have been embedded into the executable.

推荐答案

略读 Qt资源文档:

  1. 创建一个 .qrc 文件,其中包含您想要包含的资源列表
  2. 创建一个自定义构建步骤,在该文件上调用 rcc (文档)
  3. 编译生成的 cpp 源文件并将其链接到您的程序中.
  1. Create a .qrc file that contains a list of the resources you would like to include
  2. Create a custom build step that invokes rcc on that file (documentation)
  3. Compile and link the resulting cpp source file into your program.

这篇关于如何在没有 Qt 项目的情况下在 Visual Studio 中使用 Qt 资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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