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. `QML_IMPORT_PATH` has no effect
Forum Updated to NodeBB v4.3 + New Features

`QML_IMPORT_PATH` has no effect

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 4 Posters 2.2k Views 4 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.
  • S Offline
    S Offline
    SimpleY
    wrote on last edited by
    #1

    The issue

    I was trying to follow a basic tutorial for creating your own QML components library when I ran into (yet another) issue trying to import identified QML modules. As a very simple example for demonstration I have the following folder in my file system:
    aa7c9e66-a148-4d49-86a2-2d9d03d0a17b-image.png

    qmldir:

    module Buttons
    CustomButton 1.0 CustomButton_01.qml
    

    CustomButton_01.qml:

    import QtQuick
    
    Rectangle {
      anchors.fill: parent
      color: "black"
    }
    

    Now I just want to import this Buttons module in a project so that I can test and adjust the component. Of course I need to make the QML engine aware of where to look for the module and the best way to do that is the QML_IMPORT_PATH environment variable as described by the official documentation. So the best way to do that is by modifying CMakeLists.txt as follows:

    cmake_minimum_required(VERSION 3.16)
    
    project(ComponentCreator VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup(REQUIRES 6.5)
    
    ###################### QML_IMPORT_PATH  ######################
    # Doing it both with '\' and '/' just to demonstrate that's not the issue
    list(APPEND QML_DIRS "C:/Qt/CustomComponents")
    list(APPEND QML_DIRS "C:\\Qt\\CustomComponents")
    set(QML_IMPORT_PATH "${QML_DIRS}" CACHE STRING "Qt Creator QML import paths" FORCE)
    ###################### QML_IMPORT_PATH  ######################
    
    qt_add_executable(appComponentCreator
        main.cpp
    )
    
    qt_add_qml_module(appComponentCreator
        URI ComponentCreator
        VERSION 1.0
        QML_FILES
            Main.qml
    )
    
    # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
    # If you are developing for iOS or macOS you should consider setting an
    # explicit, fixed bundle identifier manually though.
    set_target_properties(appComponentCreator PROPERTIES
    #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appComponentCreator
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
        MACOSX_BUNDLE TRUE
        WIN32_EXECUTABLE TRUE
    )
    
    target_link_libraries(appComponentCreator
        PRIVATE Qt6::Quick
    )
    
    include(GNUInstallDirs)
    install(TARGETS appComponentCreator
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
    

    Now trying to use the module in Main.qml:

    import QtQuick
    import Buttons # Produces 2 warnings
    
    Window {
      width: 640
      height: 480
      visible: true
      title: qsTr("Hello World")
    }
    

    Not only do I get warnings in the Qt Creator for that import:

    C:\Users\Nigyzst\source\repos\Qt\ComponentCreator\Main.qml:2: warning: Warnings occurred while importing module "Buttons": [import]
    C:\Users\Nigyzst\source\repos\Qt\ComponentCreator\Main.qml:1: warning: Failed to import Buttons. Are your import paths set up properly? [import]
    

    But surprisingly the warnings are correct as running the application fails:
    dd2d95b7-42b0-4436-ac8a-8c1aece8192e-image.png

    Double checking the CMake configuration after running and it seems fine:
    861e4b4a-7b06-4162-990f-9f45b2e9bf92-image.png

    Question

    What am I doing wrong that the both the QML engine (at runtime) and the Language Server (in the IDE) can't find the module? I have read the official documentation and looked around on Stackoverflow but according to both resources it should work.

    Environment

    • Windows 10 64-bit
    • Qt Creator 14.0.2 (Community): all settings completely untouched
    • Qt 6.8.0
    • Project: Qt Quick Application, Kit: Destkop Qt 6.8.0 MinGW 64-bit with CMake
    1 Reply Last reply
    1
    • I Offline
      I Offline
      idrgnx
      wrote on last edited by
      #2

      Had the same problem for a while! Found nothing:/

      1 Reply Last reply
      0
      • MarkkyboyM Offline
        MarkkyboyM Offline
        Markkyboy
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • ekkescornerE Offline
          ekkescornerE Offline
          ekkescorner
          Qt Champions 2016
          wrote on last edited by
          #4

          try this:
          use qt_standard_project_setup(REQUIRES 6.8)
          this will enable policy QTP004 (https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html)

          then you don 't have to deal with import pathes, you don't have to create qmldir files - qmlls should find your QML files inside folders.

          check your QtC preferences QtQuick - Enable JS/QML Editing: check QMLLS settings
          also you can try to set QT_QML_GENERATE_QMLLS_INI fir CMake

          ekke ... Qt Champion 2016 | 2024 ... mobile business apps
          5.15 --> 6.9 https://t1p.de/ekkeChecklist
          QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

          S 1 Reply Last reply
          0
          • ekkescornerE ekkescorner

            try this:
            use qt_standard_project_setup(REQUIRES 6.8)
            this will enable policy QTP004 (https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html)

            then you don 't have to deal with import pathes, you don't have to create qmldir files - qmlls should find your QML files inside folders.

            check your QtC preferences QtQuick - Enable JS/QML Editing: check QMLLS settings
            also you can try to set QT_QML_GENERATE_QMLLS_INI fir CMake

            S Offline
            S Offline
            SimpleY
            wrote on last edited by
            #5

            @ekkescorner Thanks for the effort.

            Shortly, I tried everything you suggested but everything remained the same. Here is what exactly I did to confirm:

            1. Replace qt_standard_project_setup(REQUIRES 6.5) with qt_standard_project_setup(REQUIRES 6.8) in the CMake file
            2. Rerun, see if anything changed: nothing
            3. Check Qt Creator preferences, seem fine as they were, didn't change anything here:
              aaa795f2-5c87-4a40-80fa-57627ff6d94a-image.png
            4. Added set(QT_QML_GENERATE_QMLLS_INI ON) in CMake.
            5. Rerun and see that nothing changed.

            I am not surprised set(QT_QML_GENERATE_QMLLS_INI ON) didn't do anything as that helps only if the module I am importing is a part of the project and gets put in the build directory, but here it's not. Not sure why but QTP004 also didn't change anything.

            1 Reply Last reply
            0
            • ekkescornerE Offline
              ekkescornerE Offline
              ekkescorner
              Qt Champions 2016
              wrote on last edited by
              #6

              sorry - have overlooked that your module isn't part of the project.
              my experiences with nested QML folders are with folders inside the project, where the settings I mentioned make differences with 6.8
              ATM my projects (ported from 5.15 to 6.6...6.8) are using QMake, because I'm waiting for some fixes with 6.8.1 (end of nov '24) before doing the ports from QMake to CMake.
              this means it'll take some months until I'll run into same situations - it's on my TODO to separate common QML files to be used in my apps. From my understanding that will be extra qml_modules I have to import or link.
              but no time to test this now...

              ekke ... Qt Champion 2016 | 2024 ... mobile business apps
              5.15 --> 6.9 https://t1p.de/ekkeChecklist
              QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

              S 1 Reply Last reply
              1
              • ekkescornerE ekkescorner

                sorry - have overlooked that your module isn't part of the project.
                my experiences with nested QML folders are with folders inside the project, where the settings I mentioned make differences with 6.8
                ATM my projects (ported from 5.15 to 6.6...6.8) are using QMake, because I'm waiting for some fixes with 6.8.1 (end of nov '24) before doing the ports from QMake to CMake.
                this means it'll take some months until I'll run into same situations - it's on my TODO to separate common QML files to be used in my apps. From my understanding that will be extra qml_modules I have to import or link.
                but no time to test this now...

                S Offline
                S Offline
                SimpleY
                wrote on last edited by
                #7

                @ekkescorner Alright thanks a lot for the info. Just one question: how can I switch to usung qmake in my Qt Quick app because when I create a new project from the Qt Creator, it doesn't ask me to choose between qmake and CMake? I suppose I can manually create a .pro file and open it with Qt Creator or?

                1 Reply Last reply
                0
                • ekkescornerE Offline
                  ekkescornerE Offline
                  ekkescorner
                  Qt Champions 2016
                  wrote on last edited by
                  #8

                  Yep. Create a .pro and open from QtC

                  ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                  5.15 --> 6.9 https://t1p.de/ekkeChecklist
                  QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                  1 Reply Last reply
                  0

                  • Login

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