visual-studio Microsoft Visual Studio:在 Qt 应用程序中加载资源(无插件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1212391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:49:56  来源:igfitidea点击:

Microsoft Visual Studio: Loading resources in Qt application (without plug-in)

c++visual-studioqtresourcesqt4

提问by MadH

We don't have a Qt plug-in installed for MSVS, and it makes me wonder how/whether it is possible to load resources (images, etc) to the application.

我们没有为 MSVS 安装 Qt 插件,这让我想知道如何/是否可以将资源(图像等)加载到应用程序。

回答by Matthieu

Yes, you can load ressources. Unfortunately, the qrc Editor which create qrc files is part of the Qt Addin for VS...
But you can create this xml file by hands, for the format see here
Once the qrc file created, you have at least two possibilities :

是的,您可以加载资源。不幸的是,创建 qrc 文件的 qrc 编辑器是 VS 的 Qt Addin 的一部分...
但是您可以手动创建这个 xml 文件,格式参见此处
一旦创建了 qrc 文件,您至少有两种可能性:

A) Use qmake

A)使用 qmake

  1. Add a reference to your qrc file in your pro file :

    RESOURCES = ApplicationResources.qrc

  2. Regenerate your vcproj from your pro by using qmake

    qmake -tp vc

  1. 在 pro 文件中添加对 qrc 文件的引用:

    资源 = 应用程序资源.qrc

  2. 使用 qmake 从您的专业版重新生成您的 vcproj

    qmake -tp vc

B) If you don't generate your vcproj file from your pro file, you can :

B)如果您不从 pro 文件生成 vcproj 文件,则可以:

  1. Add manually your qrc file in your solution, for example in the following path :

    Resource Files/Res/ApplicationResources.qrc

  2. Add the following commands in the properties of the qrc file in visual studio :
    command line: $(QTDIR)\bin\rcc.exe -name ApplicationResources res\ ApplicationResources.qrc -o $(IntDir)\qrc__ ApplicationResources.cpp
    Description: RCC res/ApplicationResources.qrc
    Output: $(IntDir)\qrc__ ApplicationResources.cpp

  1. 在您的解决方案中手动添加您的 qrc 文件,例如在以下路径中:

    资源文件/Res/ApplicationResources.qrc

  2. 在visual studio中qrc文件的属性中添加以下命令:
    命令行:$(QTDIR)\bin\rcc.exe -name ApplicationResources res\ApplicationResources.qrc -o $(IntDir)\qrc__ ApplicationResources.cpp
    描述:RCC res/ApplicationResources.qrc
    输出:$(IntDir)\qrc__ ApplicationResources.cpp

C) You can also use an external binary resources file
The command line :rcc -binary myresource.qrc -o myresource.rcc

C)也可以使用外部二进制资源文件
命令行:rcc -binary myresource.qrc -o myresource.rcc

In the application, you have to register the resource file : QResource::registerResource("/path/to/myresource.rcc");

在应用程序中,您必须注册资源文件: QResource::registerResource("/path/to/myresource.rcc");

For using resource file in the source code see the doc

有关在源代码中使用资源文件,请参阅文档

However, like cheez, I also suggest using qmake and pro file and do not edit properties by hand in Visual Studio...

但是,像 cheez 一样,我也建议使用 qmake 和 pro 文件,不要在 Visual Studio 中手动编辑属性...

Hope this helps !

希望这可以帮助 !

回答by cheez

Use the qrc executable to generate a cpp file which you can include in your project:

使用 qrc 可执行文件生成可以包含在项目中的 cpp 文件:

/usr/local/Trolltech/Qt-4.5.1/bin/rcc -name core core/core.qrc -o build/release/core/qrc_core.cc

/usr/local/Trolltech/Qt-4.5.1/bin/rcc -name core core/core.qrc -o build/release/core/qrc_core.cc

See http://doc.trolltech.com/4.0/resources.html

http://doc.trolltech.com/4.0/resources.html

However, I strongly suggest using qmake or some other build system to automate this for you.

但是,我强烈建议使用 qmake 或其他一些构建系统来为您自动化。