c++ - 无法在基于cmake的qt项目中加载qml文件

标签 c++ qt cmake qml

<分区>

我正在开发一个基于 cmake 的 Qt 项目。 Qt版本为5.7,Ubuntu 14.04。我得到的错误是:

QQmlApplicationEngine failed to load component
qrc:/main.qml:-1 File not found

CMakeLists.txt 是:

file(GLOB_RECURSE UI_FILES *.ui)
file(GLOB_RECURSE CODE_FILES *.cpp *.h)

qt5_wrap_ui(UI_HEADERS ${UI_FILES})
qt5_add_resources(RESOURCE_FILES ../resources/resources.qrc)

set(SOURCES
    assets/assets.qrc
    icons/icons.qrc
    qml/qml.qrc)

include(../vendor/CMakeLists.txt)

if (WIN32)
  set(WINDOWS_RES_FILE ${CMAKE_CURRENT_BINARY_DIR}/resources.obj)
  if (MSVC)
    add_custom_command(OUTPUT ${WINDOWS_RES_FILE}
      COMMAND rc.exe /fo ${WINDOWS_RES_FILE} resources.rc
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/win
    )
  else()
    add_custom_command(OUTPUT ${WINDOWS_RES_FILE}
      COMMAND windres.exe resources.rc ${WINDOWS_RES_FILE}
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/win
    )
  endif()
endif()

add_executable(${CMAKE_PROJECT_NAME}
  ${SOURCES}
  ${UI_HEADERS}
  ${CODE_FILES}
  ${RESOURCE_FILES}
  ${WINDOWS_RES_FILE}
  ${VENDOR_SOURCES}
)

target_link_libraries(${CMAKE_PROJECT_NAME}
    Qt5::Core
    Qt5::Qml
    Qt5::Quick
    Qt5::Concurrent
    Qt5::Widgets)

find_package( PythonLibs 2.7 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )

find_package( Boost COMPONENTS python REQUIRED )
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

target_link_libraries(${CMAKE_PROJECT_NAME}
    ${PYTHON_LIBRARIES}
    ${Boost_LIBRARIES}
    ${SWORD_LIBRARIES})

include_directories(${SWORD_INCLUDE_DIRS})

set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/data/Info.plist)

if (UNIX)
  install(TARGETS ${CMAKE_PROJECT_NAME}
          RUNTIME DESTINATION bin)
elseif (WIN32)
  install(TARGETS ${CMAKE_PROJECT_NAME}
                    DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

项目的树输出是:

.
|-- build
|   |-- CMakeCache.txt
|   |-- CMakeFiles
|   |   |-- 3.6.0-rc4
|   |   |   |-- CMakeCCompiler.cmake
|   |   |   |-- CMakeCXXCompiler.cmake
|   |   |   |-- CMakeDetermineCompilerABI_C.bin
|   |   |   |-- CMakeDetermineCompilerABI_CXX.bin
|   |   |   |-- CMakeSystem.cmake
|   |   |   |-- CompilerIdC
|   |   |   |   |-- a.out
|   |   |   |   `-- CMakeCCompilerId.c
|   |   |   `-- CompilerIdCXX
|   |   |       |-- a.out
|   |   |       `-- CMakeCXXCompilerId.cpp
|   |   |-- cmake.check_cache
|   |   |-- CMakeDirectoryInformation.cmake
|   |   |-- CMakeOutput.log
|   |   |-- CMakeRuleHashes.txt
|   |   |-- CMakeTmp
|   |   |-- feature_tests.bin
|   |   |-- feature_tests.c
|   |   |-- feature_tests.cxx
|   |   |-- Makefile2
|   |   |-- Makefile.cmake
|   |   |-- Progress
|   |   |   |-- 1
|   |   |   |-- 11
|   |   |   |-- 2
|   |   |   |-- 3
|   |   |   `-- count.txt
|   |   |-- progress.marks
|   |   |-- TargetDirectories.txt
|   |   `-- uninstall.dir
|   |       |-- build.make
|   |       |-- cmake_clean.cmake
|   |       |-- DependInfo.cmake
|   |       `-- progress.make
|   |-- cmake_install.cmake
|   |-- CTestTestfile.cmake
|   |-- ecm_uninstall.cmake
|   |-- Makefile
|   |-- source
|   |   |-- __
|   |   |   `-- resources
|   |   |       `-- resources.qrc.depends
|   |   |-- CMakeFiles
|   |   |   |-- CMakeDirectoryInformation.cmake
|   |   |   |-- progress.marks
|   |   |   |-- tutifruti_automoc.dir
|   |   |   |   |-- AutogenInfo.cmake
|   |   |   |   |-- AutomocOldMocDefinitions.cmake
|   |   |   |   |-- build.make
|   |   |   |   |-- cmake_clean.cmake
|   |   |   |   |-- DependInfo.cmake
|   |   |   |   |-- depend.internal
|   |   |   |   |-- depend.make
|   |   |   |   `-- progress.make
|   |   |   `-- tutifruti.dir
|   |   |       |-- __
|   |   |       |   `-- vendor
|   |   |       |       `-- material
|   |   |       |           `-- src
|   |   |       |               `-- core
|   |   |       |-- build.make
|   |   |       |-- cmake_clean.cmake
|   |   |       |-- CXX.includecache
|   |   |       |-- DependInfo.cmake
|   |   |       |-- depend.internal
|   |   |       |-- depend.make
|   |   |       |-- flags.make
|   |   |       |-- link.txt
|   |   |       `-- progress.make
|   |   |-- cmake_install.cmake
|   |   |-- CTestTestfile.cmake
|   |   |-- defines.h
|   |   |-- Makefile
|   |   |-- moc_main_window.cpp
|   |   |-- qrc_resources.cpp
|   |   |-- tutifruti_automoc.cpp
|   |   |-- tutifruti_automoc.dir
|   |   |   `-- vendor
|   |   |       `-- material
|   |   |           `-- src
|   |   |               |-- core
|   |   |               |   |-- moc_device.cpp
|   |   |               |   `-- moc_units.cpp
|   |   |               `-- moc_plugin.cpp
|   |   `-- ui_main_window.h
|   `-- tests
|       |-- CMakeFiles
|       |   |-- CMakeDirectoryInformation.cmake
|       |   `-- progress.marks
|       |-- cmake_install.cmake
|       |-- CTestTestfile.cmake
|       |-- defines.h
|       `-- Makefile
|-- cmake
|   |-- FindCaffe.cmake
|   |-- FindSWORD.cmake
|   `-- LibFindMacros.cmake
|-- CMakeLists.txt
|-- CMakeLists.txt.user
|-- LICENCE
|-- Licence.rtf
|-- README.md
|-- resources
|   |-- icons
|   |   |-- action_home.svg
|   |   |-- action_list.svg
|   |   |-- action_search.svg
|   |   |-- action_settings.svg
|   |   |-- file_cloud_done.svg
|   |   |-- icons.qrc
|   |   |-- maps_place.svg
|   |   |-- navigation_check.svg
|   |   `-- social_school.svg
|   `-- resources.qrc
|-- source
|   |-- assets
|   |   |-- assets.qrc
|   |   |-- book-open-page.svg
|   |   `-- book-open.svg
|   |-- backend
|   |   |-- biblechapter.cpp
|   |   |-- biblechapter.h
|   |   |-- bible.cpp
|   |   |-- bible.h
|   |   |-- biblemanager.cpp
|   |   |-- biblemanager.h
|   |   |-- CMakeLists.txt
|   |   |-- module.cpp
|   |   |-- module.h
|   |   |-- plugin.cpp
|   |   |-- progress.h
|   |   `-- promise.h
|   |-- CMakeLists.txt
|   |-- cpp
|   |-- icons
|   |   |-- action_home.svg
|   |   |-- action_list.svg
|   |   |-- action_search.svg
|   |   |-- action_settings.svg
|   |   |-- file_cloud_done.svg
|   |   |-- icons.qrc
|   |   |-- maps_place.svg
|   |   |-- navigation_check.svg
|   |   `-- social_school.svg
|   |-- icons.yml
|   |-- main.cpp
|   |-- python
|   |   |-- data
|   |   |   |-- external
|   |   |   |-- interim
|   |   |   |-- processed
|   |   |   `-- raw
|   |   |-- docs
|   |   |   |-- commands.rst
|   |   |   |-- conf.py
|   |   |   |-- getting-started.rst
|   |   |   |-- index.rst
|   |   |   |-- make.bat
|   |   |   `-- Makefile
|   |   |-- LICENSE
|   |   |-- Makefile
|   |   |-- models
|   |   |-- notebooks
|   |   |-- README.md
|   |   |-- references
|   |   |-- reports
|   |   |   `-- figures
|   |   |-- requirements.txt
|   |   |-- src
|   |   |   |-- data
|   |   |   |   `-- make_dataset.py
|   |   |   |-- features
|   |   |   |   `-- build_features.py
|   |   |   |-- __init__.py
|   |   |   |-- models
|   |   |   |   |-- predict_model.py
|   |   |   |   `-- train_model.py
|   |   |   `-- visualization
|   |   |       `-- visualize.py
|   |   `-- tox.ini
|   `-- qml
|       |-- components
|       |   |-- BibleView.qml
|       |   |-- Placeholder.qml
|       |   `-- VerseDelegate.qml
|       |-- main.qml
|       |-- qml.qrc
|       `-- ui
|           |-- HomeTab.qml
|           `-- SettingsPage.qml
|-- tests
|   |-- CMakeLists.txt
|   |-- defines.h.cmake
|   `-- example_tests.cpp.example
|-- text.txt
|-- vendor
|   |-- CMakeLists.txt
|   `-- material
|       |-- CHANGELOG.md
|       |-- CONTRIBUTING.md
|       |-- demo
|       |   |-- BottomSheetDemo.qml
|       |   |-- ButtonDemo.qml
|       |   |-- CheckBoxDemo.qml
|       |   |-- ColorPaletteDemo.qml
|       |   |-- CustomIconsDemo.qml
|       |   |-- DatePickerDemo.qml
|       |   |-- demo.pro
|       |   |-- demo.qmlproject
|       |   |-- demo.qrc
|       |   |-- DialogDemo.qml
|       |   |-- FormsDemo.qml
|       |   |-- icons
|       |   |   |-- action_account_circle.svg
|       |   |   |-- action_autorenew.svg
|       |   |   |-- action_delete.svg
|       |   |   |-- action_language.svg
|       |   |   |-- action_settings.svg
|       |   |   |-- alert_warning.svg
|       |   |   |-- communication_email.svg
|       |   |   |-- content_add.svg
|       |   |   |-- content_create.svg
|       |   |   |-- content_forward.svg
|       |   |   |-- device_access_alarm.svg
|       |   |   |-- file_file_download.svg
|       |   |   |-- icons.qrc
|       |   |   |-- image_color_lens.svg
|       |   |   |-- image_edit.svg
|       |   |   |-- maps_place.svg
|       |   |   |-- navigation_arrow_drop_down.svg
|       |   |   `-- social_share.svg
|       |   |-- icons.yml
|       |   |-- images
|       |   |   |-- balloon.jpg
|       |   |   |-- go-last.color.svg
|       |   |   |-- list-add.color.svg
|       |   |   |-- weather-pouring.svg
|       |   |   `-- weather-sunset.svg
|       |   |-- ListItemsDemo.qml
|       |   |-- main.cpp
|       |   |-- main.qml
|       |   |-- PageStackDemo.qml
|       |   |-- ProgressBarDemo.qml
|       |   |-- RadioButtonDemo.qml
|       |   |-- SidebarPage.qml
|       |   |-- SliderDemo.qml
|       |   |-- SubPage.qml
|       |   |-- SwitchDemo.qml
|       |   |-- TextFieldDemo.qml
|       |   |-- TimePickerDemo.qml
|       |   `-- TypographyDemo.qml
|       |-- deploy_key.enc
|       |-- documentation
|       |   |-- images
|       |   |   `-- buttons.png
|       |   |-- material.qdoc
|       |   `-- material.qdocconf
|       |-- fonts
|       |   |-- fonts.qrc
|       |   |-- MaterialFontLoader.qml
|       |   |-- qmldir
|       |   `-- roboto
|       |       |-- Roboto-BlackItalic.ttf
|       |       |-- Roboto-Black.ttf
|       |       |-- Roboto-BoldItalic.ttf
|       |       |-- Roboto-Bold.ttf
|       |       |-- RobotoCondensed-BoldItalic.ttf
|       |       |-- RobotoCondensed-Bold.ttf
|       |       |-- RobotoCondensed-Italic.ttf
|       |       |-- RobotoCondensed-LightItalic.ttf
|       |       |-- RobotoCondensed-Light.ttf
|       |       |-- RobotoCondensed-Regular.ttf
|       |       |-- Roboto-Italic.ttf
|       |       |-- Roboto-LightItalic.ttf
|       |       |-- Roboto-Light.ttf
|       |       |-- Roboto-MediumItalic.ttf
|       |       |-- Roboto-Medium.ttf
|       |       |-- Roboto-Regular.ttf
|       |       |-- Roboto-ThinItalic.ttf
|       |       `-- Roboto-Thin.ttf
|       |-- icons
|       |   |-- core_icons.qrc
|       |   |-- navigation_arrow_back.svg
|       |   |-- navigation_chevron_left.svg
|       |   |-- navigation_chevron_right.svg
|       |   |-- navigation_close.svg
|       |   |-- navigation_menu.svg
|       |   `-- navigation_more_vert.svg
|       |-- icons.yml
|       |-- LICENSE
|       |-- LICENSE.CC-BY
|       |-- LICENSE.MPL
|       |-- material.pri
|       |-- qml-material.pro
|       |-- qpm.json
|       |-- README.md
|       |-- scripts
|       |   |-- build_docs.sh
|       |   |-- deploy.sh
|       |   |-- icons.py
|       |   |-- lint.sh
|       |   |-- make_awesome.py
|       |   |-- normalize_imports.sh
|       |   `-- qrc.py
|       |-- src
|       |   |-- components
|       |   |   |-- ActionButton.qml
|       |   |   |-- Card.qml
|       |   |   |-- components.qrc
|       |   |   |-- DatePicker.qml
|       |   |   |-- IconButton.qml
|       |   |   |-- OverlayLayer.qml
|       |   |   |-- OverlayView.qml
|       |   |   |-- ProgressCircle.qml
|       |   |   |-- Scrollbar.qml
|       |   |   |-- Snackbar.qml
|       |   |   |-- ThinDivider.qml
|       |   |   |-- TimePicker.qml
|       |   |   |-- Tooltip.qml
|       |   |   `-- Wave.qml
|       |   |-- controls
|       |   |   |-- Action.qml
|       |   |   |-- Button.qml
|       |   |   |-- CheckBox.qml
|       |   |   |-- controls.qrc
|       |   |   |-- Label.qml
|       |   |   |-- ProgressBar.qml
|       |   |   |-- RadioButton.qml
|       |   |   |-- Slider.qml
|       |   |   |-- Switch.qml
|       |   |   |-- Tab.qml
|       |   |   `-- TextField.qml
|       |   |-- core
|       |   |   |-- AwesomeIcon.qml
|       |   |   |-- awesome.js
|       |   |   |-- core.qrc
|       |   |   |-- device.cpp
|       |   |   |-- device.h
|       |   |   |-- FontAwesome.otf
|       |   |   |-- Icon.qml
|       |   |   |-- Ink.qml
|       |   |   |-- MaterialAnimation.qml
|       |   |   |-- Object.qml
|       |   |   |-- Palette.qml
|       |   |   |-- PlatformExtensions.qml
|       |   |   |-- ThemePalette.qml
|       |   |   |-- Theme.qml
|       |   |   |-- units.cpp
|       |   |   |-- units.h
|       |   |   |-- UnitsHelper.qml
|       |   |   |-- utils.js
|       |   |   `-- View.qml
|       |   |-- extras
|       |   |   |-- AutomaticGrid.qml
|       |   |   |-- CircleImage.qml
|       |   |   |-- CircleMask.qml
|       |   |   |-- ColumnFlow.qml
|       |   |   |-- extras.qrc
|       |   |   |-- Image.qml
|       |   |   `-- qmldir
|       |   |-- listitems
|       |   |   |-- BaseListItem.qml
|       |   |   |-- CMakeLists.txt
|       |   |   |-- Divider.qml
|       |   |   |-- listitems.qrc
|       |   |   |-- qmldir
|       |   |   |-- SectionHeader.qml
|       |   |   |-- SimpleMenu.qml
|       |   |   |-- Standard.qml
|       |   |   |-- Subheader.qml
|       |   |   `-- Subtitled.qml
|       |   |-- material.qrc
|       |   |-- plugin.cpp
|       |   |-- plugin.h
|       |   |-- popups
|       |   |   |-- BottomActionSheet.qml
|       |   |   |-- BottomSheet.qml
|       |   |   |-- Dialog.qml
|       |   |   |-- Dropdown.qml
|       |   |   |-- InputDialog.qml
|       |   |   |-- MenuField.qml
|       |   |   |-- Popover.qml
|       |   |   |-- PopupBase.qml
|       |   |   |-- popups.qrc
|       |   |   `-- TimePickerDialog.qml
|       |   |-- qmldir
|       |   |-- src.pro
|       |   |-- styles
|       |   |   |-- ApplicationWindowStyle.qml
|       |   |   |-- ButtonStyle.qml
|       |   |   |-- CheckBoxStyle.qml
|       |   |   |-- CMakeLists.txt
|       |   |   |-- ProgressBarStyle.qml
|       |   |   |-- qmldir
|       |   |   |-- RadioButtonStyle.qml
|       |   |   |-- SliderStyle.qml
|       |   |   |-- styles.qrc
|       |   |   |-- SwitchStyle.qml
|       |   |   |-- TextFieldStyle.qml
|       |   |   |-- ToolBarStyle.qml
|       |   |   `-- ToolButtonStyle.qml
|       |   `-- window
|       |       |-- ActionBar.qml
|       |       |-- ApplicationWindow.qml
|       |       |-- AppTheme.qml
|       |       |-- MainView.qml
|       |       |-- NavigationDrawerPage.qml
|       |       |-- NavigationDrawer.qml
|       |       |-- Page.qml
|       |       |-- PageSidebar.qml
|       |       |-- PageStack.qml
|       |       |-- Sidebar.qml
|       |       |-- TabBar.qml
|       |       |-- TabbedPage.qml
|       |       |-- Toolbar.qml
|       |       |-- Window.qml
|       |       `-- window.qrc
|       |-- styles_demo
|       |   |-- main.qml
|       |   `-- Makefile
|       |-- tests
|       |   |-- icons
|       |   |   |-- action_alarm.svg
|       |   |   |-- action_search.svg
|       |   |   |-- action_settings.svg
|       |   |   |-- content_add.svg
|       |   |   `-- icons.qrc
|       |   |-- icons.yml
|       |   |-- tests.cpp
|       |   |-- tests.pro
|       |   |-- tst_actionbar.qml
|       |   |-- tst_card.qml
|       |   `-- tst_pagestack.qml
|       `-- vendor.cmake
`-- win
    |-- appicon.ico
    |-- CMakeLists.txt
    |-- installer.cmake.nsi
    |-- installer.cmake.wxs
    |-- qt.conf
    `-- resources.rc

如何从项目中删除此错误?

最佳答案

您可以使用QDirIterator 仔细检查应用程序包含哪些资源。例如在 main() 中:

QDirIterator it(":/", QDirIterator::Subdirectories);
while (it.hasNext())
    qDebug() << it.next();

在不知道 Qt 资源处理如何与 CMake 一起工作的情况下,我会说以下几行看起来像是罪魁祸首。

qt5_add_resources(RESOURCE_FILES ../resources/resources.qrc)

set(SOURCES
    assets/assets.qrc
    icons/icons.qrc
    qml/qml.qrc)

为什么一个.qrcqt5_add_resources()添加,而其他的添加到SOURCES

关于c++ - 无法在基于cmake的qt项目中加载qml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39056277/

相关文章:

c++ - libcurl,如何为每个句柄附加不同的回调函数?

c++ - 为什么 C++ 编译器合成函数(例如默认构造函数)是内联的?

c++ - 我真的必须链接 Ws2_32.lib 吗?

c++ - 使用 memcpy 复制从 QGraphicsItem 派生的类不会创建新对象

c++ - cmake:构建一个程序的多个版本

c++ - 在 makefile 中,目录名称是假目标还是 "real"目标?

c++ - QCheckBox 检测标签上的点击

c++ - 如何将 Switch Qml 添加到 qt widgets?

c++ - 将 FetchContent_Declare 与 CMAKE_ARGS 一起使用

c++ - 通过 dll 边界传递对 STL vector 的引用