Header not found in <appName>_qmltyperegistrations.cpp
-
Hello,
in Qt5 I used for a Qt Quick Application the following approach to connect a c++ model with a qml listview:
qmlRegisterType<UserModel>("UserModel",1,0,"UserModel");
Whereby the usermodel.h contained
Q_OBJECT QML_ELEMENT
However, the same approach doesn't work in qt6 and to be honest I don't understand what the exact equivalent in qt6 would be.
Now I am using CMake (seems to be the new standard?) and I have the following CMakeLists.txt (where I changed nothing except the header and cpp files...):
cmake_minimum_required(VERSION 3.21.1) option(LINK_INSIGHT "Link Qt Insight Tracker library" ON) option(BUILD_QDS_COMPONENTS "Build design studio components" ON) project(AppName LANGUAGES CXX) set(CMAKE_AUTOMOC ON) find_package(Qt6 6.2 REQUIRED COMPONENTS Core Gui Qml Quick Sql) if (Qt6_VERSION VERSION_GREATER_EQUAL 6.3) qt_standard_project_setup() endif() qt_add_executable(<appName> src/main.cpp <...Some more cpp and header... > src/usermodel.h src/usermodel.cpp ) qt_add_resources(StockforecastApp "configuration" PREFIX "/" FILES qtquickcontrols2.conf ) target_link_libraries(AppName PUBLIC Qt6::Core Qt6::Gui Qt6::Qml Qt6::Quick Qt6::Sql ) if (BUILD_QDS_COMPONENTS) include(${CMAKE_CURRENT_SOURCE_DIR}/qmlcomponents) endif() include(${CMAKE_CURRENT_SOURCE_DIR}/qmlmodules) if (LINK_INSIGHT) include(${CMAKE_CURRENT_SOURCE_DIR}/insight) endif () include(GNUInstallDirs) install(TARGETS AppName BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) # make IDEs aware of the QML import path set(QML_IMPORT_PATH ${PROJECT_BINARY_DIR}/qml CACHE PATH "Path to the custom QML components defined by the project")
..The header contains again the macros mentioned above. However I get this error:
<pathAndAppName>_qmltyperegistrations.cpp:10:10: fatal error: usermodel.h: No such file or directory 10 | #include <usermodel.h> | ^~~~~~~~~~~~~
I took a look at the Qt-examples and I foudnd "contactlist" which does in generel the same like I want to do.
The CMakeLists.txt there looks like:
# Copyright (C) 2023 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(contactlist LANGUAGES CXX) set(CMAKE_AUTOMOC ON) if(NOT DEFINED INSTALL_EXAMPLESDIR) set(INSTALL_EXAMPLESDIR "examples") endif() set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quickcontrols/contactlist") find_package(Qt6 REQUIRED COMPONENTS Core Gui Quick) qt_add_executable(contactlistexample WIN32 MACOSX_BUNDLE contactmodel.cpp contactmodel.h main.cpp ) qt_add_qml_module(contactlistexample URI contactlist NO_RESOURCE_TARGET_PATH QML_FILES "ContactDelegate.ui.qml" "ContactDialog.qml" "ContactForm.ui.qml" "ContactView.ui.qml" "SectionDelegate.ui.qml" "ContactList.qml" "designer/Backend/ContactModel.qml" ) target_link_libraries(contactlistexample PUBLIC Qt6::Core Qt6::Gui Qt6::Quick ) install(TARGETS contactlistexample RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" )
...So why is this running and my app not?. I guess I need as well qt_add_qml_module but I am not sure. Why should I need now a module and in the past not? What is missing in my CMakeLists.txt? Or is there another error?
Thanks!
-
You do not set the include folder where the compiler should search for header files.
See https://cmake.org/cmake/help/latest/command/target_include_directories.html#command:target_include_directories -