Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Header not found in <appName>_qmltyperegistrations.cpp

Header not found in <appName>_qmltyperegistrations.cpp

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qml c++listviewcmakelists.txtcmake
3 Posts 2 Posters 622 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    firen
    wrote on 7 May 2024, 18:06 last edited by firen 5 Jul 2024, 18:10
    #1

    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!

    1 Reply Last reply
    1
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 8 May 2024, 07:07 last edited by
      #2

      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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply 8 May 2024, 10:23
      1
      • J jsulm
        8 May 2024, 07:07

        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

        F Offline
        F Offline
        firen
        wrote on 8 May 2024, 10:23 last edited by firen 5 Aug 2024, 10:24
        #3

        @jsulm thanks!

        I just added

        target_include_directories(<appName> PUBLIC src)
        

        what works well.

        1 Reply Last reply
        0
        • F firen has marked this topic as solved on 8 May 2024, 10:24

        2/3

        8 May 2024, 07:07

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved