如何在使用CMake作为构建系统的QT 6小部件应用程序中正确添加.qrc文件?

huangapple go评论58阅读模式
英文:

How to properly add a .qrc file to a QT 6 widget app with Cmake as building system?

问题

I'm trying to use a .qrc file to store icon images for my QT application but the Resources.qrc file never appears in resource selection. I'm using CMake as the building system and QT version 6.3.1.

When simply adding Resource.qrc to source files with AUTORCC ON, the file never appears in the project tree. When specifically written as a project source in CMakeLists, it appears in the project tree but never in the resource selection when I'm trying to set the ToolButton icon, regardless of whether I add a prefix and images or not.

When AUTORCC is OFF and qt_add_resources(PROJECT_SOURCES Resources.qrc) is added to CMakeLists, the Resources.qrc file appears in the CMake modules folder in a tree, but again, never in the resource selection.

There are a lot of guides about .qrc when QMake is the building system, but I hardly found anything about the same thing with CMake, and none of them helped me. Maybe I couldn't understand them properly. Sorry for any possible mistakes; I'm new to programming, and English isn't my native language. I'll be grateful for any help.

英文:

I'm trying to use a .qrc file to store icon images for my QT application but the Resources.qrc file never appears in resource selection. I'm using CMake as building system and 6.3.1 QT version.

When simply adding Resource.qrc to source files with AUTORCC ON file never appears in project tree, when specifically written as project source in CMakeLists it appears in project tree, but never in resource selection when I'm trying to set ToolButton icon, no matter if I add prefix and images or not.

When AUTORCC is OFF and qt_add_resources(PROJECT_SOURCES Resources.qrc) added to CMakeLists, Resources.qrc file appears in CMake modules folder in a tree, but again, never in resource selection.

There are a lot of guides about .qrc when QMake is building system, but I hardly found anything about same thing with CMake, and none of them helped me, maybe I couldn't understand them properly. Sorry for possible mistakes, I'm new in programming and english isn't my native language. I'll be grateful for any help.

答案1

得分: 1

以下是翻译好的部分:

我刚刚花了几个小时来弄清楚如何在Qt Quick应用程序中简单地导入图像。以下是对我有效的方法。

我要添加的图像位于/my-project/assets/icon.png/my-project/assets/icon-with-alias.png

我在项目的根目录/my-project/images.qrc中创建了一个资源文件:

<RCC>
    <qresource prefix="/images">
        <file>assets/icon.png</file>
        <file alias="alias.png">assets/icon-with-alias.png</file>
    </qresource>
</RCC>

然后在CMakeLists.txt中:

set(CMAKE_AUTORCC ON)

qt_add_executable(your_app_name_here
    main.cpp
    images.qrc
)

从现在开始,你的.qrc文件应该在项目树中可见。
不幸的是,我无法让图像与Qt工具栏一起使用。
不过,你可以在Main.qml中这样使用图像:

Image {
    id: img
    source: "/images/assets/icon.png"
}

Image {
    id: img-with-alias
    source: "/images/alias.png"
}

希望这对你有所帮助。

英文:

I just struggled for few hours figuring how to simply import an image in a Qt Quick application. Here's what worked for me.

The images I want to add are located at /my-project/assets/icon.png and /my-project/assets/icon-with-alias.png.

I created a resource file at the project's root /my-project/images.qrc :

&lt;RCC&gt;
    &lt;qresource prefix=&quot;/images&quot;&gt;
        &lt;file&gt;assets/icon.png&lt;/file&gt;
        &lt;file alias=&quot;alias.png&quot;&gt;assets/icon-with-alias.png&lt;/file&gt;
    &lt;/qresource&gt;
&lt;/RCC&gt;

Then in CMakeLists.txt:

set(CMAKE_AUTORCC ON)

qt_add_executable(your_app_name_here
    main.cpp
    images.qrc
)

From nom on, your .qrc file should be visible in the project tree.
Unfortunately I didn't manage to make the image usable with Qt Toolbar for images.
Still, you can use the image by doing in Main.qml:

Image {
    id: img
    source: &quot;/images/assets/icon.png&quot;
}

Image {
    id: img-with-alias
    source: &quot;/images/alias.png&quot;
}

Hope this help a bit.

huangapple
  • 本文由 发表于 2023年2月8日 22:20:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定