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. Usage of IMPORT_PATH in qt_add_qml_module
Forum Updated to NodeBB v4.3 + New Features

Usage of IMPORT_PATH in qt_add_qml_module

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 3.6k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    I can't seem to get the IMPORT_PATH given in qt_add_qml_module to work. Here is what I am trying do do:

    I use cmake and qt_add_qml_module to add multiple static QML modules in my project (Qt 6.5.1). Some of these modules depend on each other.

    So let's say I have QML module "MyCompany.Abc" which depends on "MyCompany.Core".
    In the qt_add_qml_module for "MyCompany.Abc" I add

    IMPORTS "MyCompany.Core"
    

    However, when editing a QML file in "MyCompany.Abc", I get
    "Implicit import 'MyCompany.Core' or QML module 'MyCompany.Abc' not found"

    It also mentions that the QML import path may be missing, and shows that only '<Qt install path>/qml' is in the import paths.

    I thought this should be easy to fix by adding IMPORT_PATH to qt_add_qml_module. So I added the root path of all my QML modules (located in the build path, because that's where the QML modules get generated). But that seems to be totally ignored:

    • It doesn't fix the error I see in QML editor (and no additional import path is shown in the error message)
    • The path doesn't seem to be added to the generated qmldir file

    How do I use IMPORT_PATH correctly?

    MesrineM JKSHJ 2 Replies Last reply
    1
    • A Asperamanca

      I can't seem to get the IMPORT_PATH given in qt_add_qml_module to work. Here is what I am trying do do:

      I use cmake and qt_add_qml_module to add multiple static QML modules in my project (Qt 6.5.1). Some of these modules depend on each other.

      So let's say I have QML module "MyCompany.Abc" which depends on "MyCompany.Core".
      In the qt_add_qml_module for "MyCompany.Abc" I add

      IMPORTS "MyCompany.Core"
      

      However, when editing a QML file in "MyCompany.Abc", I get
      "Implicit import 'MyCompany.Core' or QML module 'MyCompany.Abc' not found"

      It also mentions that the QML import path may be missing, and shows that only '<Qt install path>/qml' is in the import paths.

      I thought this should be easy to fix by adding IMPORT_PATH to qt_add_qml_module. So I added the root path of all my QML modules (located in the build path, because that's where the QML modules get generated). But that seems to be totally ignored:

      • It doesn't fix the error I see in QML editor (and no additional import path is shown in the error message)
      • The path doesn't seem to be added to the generated qmldir file

      How do I use IMPORT_PATH correctly?

      MesrineM Offline
      MesrineM Offline
      Mesrine
      wrote on last edited by Mesrine
      #2

      @Asperamanca

      I find IMPORT_PATH, IMPORTS, OPTIONAL_IMPORTS complicated to understand.
      As explained here
      'If a QML file imports this module, it also imports all the modules listed under IMPORTS.'
      My understanding is that this should only be used to tell the engine that if someone import the root module is also implicitly importing its imports. It is like public linking in CMake.
      One should not use IMPORTS only because your root module import other module.
      I think your problem could be related to not adding the build path of the module to the 'QML_IMPORT_PATH' variable of CMake that qtCreator use for searching the modules.

      Normally what i do in cmake is to append my module paths to this variable like

      list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/)
              list(REMOVE_DUPLICATES QML_IMPORT_PATH)
              set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
      

      This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.

      A 1 Reply Last reply
      0
      • MesrineM Mesrine

        @Asperamanca

        I find IMPORT_PATH, IMPORTS, OPTIONAL_IMPORTS complicated to understand.
        As explained here
        'If a QML file imports this module, it also imports all the modules listed under IMPORTS.'
        My understanding is that this should only be used to tell the engine that if someone import the root module is also implicitly importing its imports. It is like public linking in CMake.
        One should not use IMPORTS only because your root module import other module.
        I think your problem could be related to not adding the build path of the module to the 'QML_IMPORT_PATH' variable of CMake that qtCreator use for searching the modules.

        Normally what i do in cmake is to append my module paths to this variable like

        list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/)
                list(REMOVE_DUPLICATES QML_IMPORT_PATH)
                set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
        

        This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.

        A Offline
        A Offline
        Asperamanca
        wrote on last edited by Asperamanca
        #3

        @Mesrine
        In the end, I use a variant of set(QML_IMPORT_PATH...) similar to what you suggested.
        It seems that if I add the same path via set(QML_IMPORT_PATH...) it works. If I add the path via qt_add_qml_module (parameter IMPORT_PATH ) it doesn't.

        I don't feel like writing the (rather complicated) sample project that would be needed in order to file this as a bug, so I guess I'll be happy to have a way that works...

        EDIT: It seems that not a single of Qt's examples uses the qt_add_qml_module with IMPORT_PATH. Tells a lot...

        1 Reply Last reply
        0
        • A Asperamanca

          I can't seem to get the IMPORT_PATH given in qt_add_qml_module to work. Here is what I am trying do do:

          I use cmake and qt_add_qml_module to add multiple static QML modules in my project (Qt 6.5.1). Some of these modules depend on each other.

          So let's say I have QML module "MyCompany.Abc" which depends on "MyCompany.Core".
          In the qt_add_qml_module for "MyCompany.Abc" I add

          IMPORTS "MyCompany.Core"
          

          However, when editing a QML file in "MyCompany.Abc", I get
          "Implicit import 'MyCompany.Core' or QML module 'MyCompany.Abc' not found"

          It also mentions that the QML import path may be missing, and shows that only '<Qt install path>/qml' is in the import paths.

          I thought this should be easy to fix by adding IMPORT_PATH to qt_add_qml_module. So I added the root path of all my QML modules (located in the build path, because that's where the QML modules get generated). But that seems to be totally ignored:

          • It doesn't fix the error I see in QML editor (and no additional import path is shown in the error message)
          • The path doesn't seem to be added to the generated qmldir file

          How do I use IMPORT_PATH correctly?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @Asperamanca said in Usage of IMPORT_PATH in qt_add_qml_module:

          It doesn't fix the error I see in QML editor

          It is not intuitive indeed. But the setting IMPORT_PATH has no effect on your editor because:

          • QML_IMPORT_PATH is for the editor
          • IMPORT_PATH is for the build-time tools (such as qmllint or qmlcachegen)

          @Mesrine said in Usage of IMPORT_PATH in qt_add_qml_module:

          Normally what i do in cmake is to append my module paths to this variable like

          list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/)
                  list(REMOVE_DUPLICATES QML_IMPORT_PATH)
                  set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
          

          This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.

          Use this instead to avoid duplicates:

          set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml)
          set(QML_IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY} CACHE STRING "Import paths for Qt Creator's code model" FORCE)
          

          It works with all kinds of layouts. Plus, you can now add IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY} to your modules and the tools will also work.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          A 1 Reply Last reply
          4
          • JKSHJ JKSH

            @Asperamanca said in Usage of IMPORT_PATH in qt_add_qml_module:

            It doesn't fix the error I see in QML editor

            It is not intuitive indeed. But the setting IMPORT_PATH has no effect on your editor because:

            • QML_IMPORT_PATH is for the editor
            • IMPORT_PATH is for the build-time tools (such as qmllint or qmlcachegen)

            @Mesrine said in Usage of IMPORT_PATH in qt_add_qml_module:

            Normally what i do in cmake is to append my module paths to this variable like

            list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/)
                    list(REMOVE_DUPLICATES QML_IMPORT_PATH)
                    set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
            

            This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.

            Use this instead to avoid duplicates:

            set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml)
            set(QML_IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY} CACHE STRING "Import paths for Qt Creator's code model" FORCE)
            

            It works with all kinds of layouts. Plus, you can now add IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY} to your modules and the tools will also work.

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @JKSH
            Thank you! I kind of did what you suggested, but now I understand better, why.

            1 Reply Last reply
            0
            • A Asperamanca has marked this topic as solved on

            • Login

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