简体   繁体   English

无法加载Qt5 / cmake Project中Qt资源文件中指定的图像

[英]Can not load image specified in Qt resource file in Qt5/cmake Project

I just started a Qt5 project using cmake as build system. 我刚刚使用cmake作为构建系统启动了一个Qt5项目。 So far this working quite well but I have problems with an icon that I want to load from a qrc file. 到目前为止,这工作得很好,但我有一个问题,我想从qrc文件加载一个图标。

Project structure: 项目结构:

CMakeLists.txt
-- icons/
   CMakeLists.txt
   icons.qrc
   locked.png
-- src/
   CMakeLists.txt
   source files...

Top Level CMakeLists.txt: 顶级CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.11) #2.8.11 provides an easy way to build with qt5
project(fluchOmat)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library. This has dependencies on QtGui and QtCore!
find_package(Qt5Widgets REQUIRED)
# Find the QtMultimedia module
find_package(Qt5Multimedia REQUIRED)

add_subdirectory(icons)
add_subdirectory(src)

CMakeLists.txt in icons/ 图标中的CMakeLists.txt /

set(RESOURCE
    ${CMAKE_CURRENT_SOURCE_DIR}/icons.qrc
)

qt5_add_resources(RESOURCE_ADDED ${RESOURCE})

icons.qrc in icons/ 图标中的icons.qrc /

<RCC>
    <qresource prefix="/">
        <file alias="locked">locked.png</file>
    </qresource>
</RCC>

CMakeLists.txt in src/ (important part) src /中的CMakeLists.txt(重要部分)

add_executable(fluchOmat ${SOURCES} ${HEADERS} ${fluchOmat_FORMS} ${RESOURCES_ADDED})

Some sample code 一些示例代码

QPixmap pm(":/locked.png");
qDebug("Width: " + pm.width());

returns nothing, so obviously this didn't work. 没有任何回报,显然这没有用。

There aren't many resources out there for Qt5 and cmake. Qt5和cmake的资源不多。 Can anyone help me with this? 谁能帮我这个? Is this a cmake problem? 这是一个cmake问题吗? For example I am not sure if qt5_add_resources is at the right location. 例如,我不确定qt5_add_resources是否位于正确的位置。 Is my qrc file wrong? 我的qrc文件错了吗?

Any help is greatly appreciated! 任何帮助是极大的赞赏!

The problem lies in the scoping rules of CMake. 问题在于CMake的范围规则。 each add_subdirectory call creates its own scope. 每个add_subdirectory调用都会创建自己的范围。 Variables declared in such a subdirectory aren't automatically populated up to the parent scope. 在此类子目录中声明的变量不会自动填充到父作用域。

So the problem is that while RESOURCE_ADDED contains a valid path in the scope of icons/CMakeLists.txt, it doesn't in the root CMakeLists.txt and hence it is an empty variable by the time you use it in src/CMakeLists.txt. 所以问题是虽然RESOURCE_ADDED在icons / CMakeLists.txt范围内包含一个有效路径,但它不在根CMakeLists.txt中,因此当你在src / CMakeLists.txt中使用它时它是一个空变量。

To raise the variable up a scope, in icons/CMakeLists.txt you can do: 要将变量提升到范围,可以在icons / CMakeLists.txt中执行以下操作:

qt5_add_resources(RESOURCE_ADDED ${RESOURCE})
set(RESOURCE_ADDED ${RESOURCE_ADDED} PARENT_SCOPE)

There's another slight problem now though! 现在还有另一个小问题!

While this will contain a valid value in src/CMakeLists.txt, it points to a file which doesn't yet exist. 虽然这将包含src / CMakeLists.txt中的有效值,但它指向一个尚不存在的文件。 The qt5_add_resources function must apply the GENERATED source file property to the variable. qt5_add_resources函数必须将GENERATED源文件属性应用于变量。 This property is not carried forward to the variable set in the parent scope. 此属性不会转发到父作用域中设置的变量。

Since add_executable expects files to exist by default, you'll need to reapply the GENERATED property to the variable in the parent scope. 由于add_executable默认情况下存在文件,因此您需要将GENERATED属性重新应用于父作用域中的变量。 You can do this eg in the src/CMakeLists.txt like this: 您可以在src / CMakeLists.txt中执行此操作,如下所示:

set_source_files_properties(${RESOURCE_ADDED} PROPERTIES GENERATED ON)
add_executable(fluchOmat ... ${RESOURCE_ADDED})

I'm not sure if the qt5_add_resources adds any other properties - if so, you'd maybe also have to reapply these. 我不确定qt5_add_resources添加了任何其他属性 - 如果是这样,您可能还需要重新应用这些属性。

I'd guess the easiest way to avoid this would be to not use add_subdirectory(icons) , and instead just move all the Qt-related CMake code to src/CMakeLists.txt: 我想最简单的方法是避免使用add_subdirectory(icons) ,而只是将所有与Qt相关的CMake代码移到src / CMakeLists.txt:

set(RESOURCE ${CMAKE_SOURCE_DIR}/icons/icons.qrc)
qt5_add_resources(RESOURCE_ADDED ${RESOURCE})
add_executable(fluchOmat ... ${RESOURCE_ADDED})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM