Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Not able to import custom QML components
Forum Updated to NodeBB v4.3 + New Features

Not able to import custom QML components

Scheduled Pinned Locked Moved Unsolved Qt 6
9 Posts 2 Posters 2.0k Views 1 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
    Ash V
    wrote on 7 Dec 2023, 14:54 last edited by
    #1

    I am using Qt 6.5 and I am not able to use my custom QML components.

    I have tried to make a change in my CMake file but it doesn't work.
    I have also tried qt_add_qml_module but it too gives error.

    25fda533-ec20-41b2-8481-22862ea201cd-Screenshot from 2023-12-07 10-06-10.png
    636c7552-6898-483a-9fb3-d1d29d8d6a0e-Screenshot from 2023-12-07 10-06-44.png

    ERROR:
    df9f8617-ca92-456d-9771-b7649eeaf1a8-Screenshot from 2023-12-07 20-24-04.png

    A 1 Reply Last reply 7 Dec 2023, 17:27
    0
    • A Ash V
      7 Dec 2023, 14:54

      I am using Qt 6.5 and I am not able to use my custom QML components.

      I have tried to make a change in my CMake file but it doesn't work.
      I have also tried qt_add_qml_module but it too gives error.

      25fda533-ec20-41b2-8481-22862ea201cd-Screenshot from 2023-12-07 10-06-10.png
      636c7552-6898-483a-9fb3-d1d29d8d6a0e-Screenshot from 2023-12-07 10-06-44.png

      ERROR:
      df9f8617-ca92-456d-9771-b7649eeaf1a8-Screenshot from 2023-12-07 20-24-04.png

      A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 7 Dec 2023, 17:27 last edited by
      #2

      @Ash-V
      The CMake file looks fine at first glance.
      The error looks like a runtime error in Qt Creator’s debug mode after a successful build.
      The QML engine doesn’t find the file containing the custom type. That has to be fixed either in main.qml or in main.cpp.
      Please post those….

      Software Engineer
      The Qt Company, Oslo

      A 1 Reply Last reply 7 Dec 2023, 17:53
      0
      • A Axel Spoerl
        7 Dec 2023, 17:27

        @Ash-V
        The CMake file looks fine at first glance.
        The error looks like a runtime error in Qt Creator’s debug mode after a successful build.
        The QML engine doesn’t find the file containing the custom type. That has to be fixed either in main.qml or in main.cpp.
        Please post those….

        A Offline
        A Offline
        Ash V
        wrote on 7 Dec 2023, 17:53 last edited by Ash V 12 Jul 2023, 17:53
        #3

        @Axel-Spoerl
        I am posting here both of the files that you've mentioned. Please provide me with the needed help to resolve this error. And yeah, this error really "does come after a successful build" in the Qt Creator IDE.

        main.qml

        f8410748-18d2-42f2-bb03-53751959a7ed-Screenshot from 2023-12-07 23-17-07.png
        8b2d39b2-f1cd-415e-8faa-c041f15a6f60-Screenshot from 2023-12-07 23-17-24.png

        main.cpp

        845a6906-c913-44c1-a856-f7ffb0cbe8f1-Screenshot from 2023-12-07 23-17-46.png

        A 1 Reply Last reply 8 Dec 2023, 06:25
        0
        • A Ash V
          7 Dec 2023, 17:53

          @Axel-Spoerl
          I am posting here both of the files that you've mentioned. Please provide me with the needed help to resolve this error. And yeah, this error really "does come after a successful build" in the Qt Creator IDE.

          main.qml

          f8410748-18d2-42f2-bb03-53751959a7ed-Screenshot from 2023-12-07 23-17-07.png
          8b2d39b2-f1cd-415e-8faa-c041f15a6f60-Screenshot from 2023-12-07 23-17-24.png

          main.cpp

          845a6906-c913-44c1-a856-f7ffb0cbe8f1-Screenshot from 2023-12-07 23-17-46.png

          A Offline
          A Offline
          Axel Spoerl
          Moderators
          wrote on 8 Dec 2023, 06:25 last edited by Axel Spoerl 12 Aug 2023, 06:27
          #4

          @Ash-V
          Thanks for sharing.

          There are two ways of adding QML files to your project.
          As you said, there is qt_add_qml_module, which is the modern API we recommend to use.
          You can refer to the quick gallery example and adapt the CMakeLists.txt to your needs.

          This project expects QML sources to be resource files. For the QML part to be dealt with by the IDE's code model, QML files have to be known to the IDE and be part of the build. Including AudioInfoBox.qmlin the qt_add_executabledirective takes care of the former, but not of the latter. This is why the file is not installed in the build tree. main.cpp expects it at ":/", but the only file in that resource tree is quickcontrols2.conf.

          => The quick solution is to add AudioInfoBox.qml right below quickcontrols2.conf in the qt_add_resourcesdeclaration.

          => The nicer solution is to refactor the CMakeLists.txtand use qt_add_qml_module. If you use the example as a template, there shouldn't be any errors.

          Software Engineer
          The Qt Company, Oslo

          A 1 Reply Last reply 8 Dec 2023, 08:27
          0
          • A Axel Spoerl
            8 Dec 2023, 06:25

            @Ash-V
            Thanks for sharing.

            There are two ways of adding QML files to your project.
            As you said, there is qt_add_qml_module, which is the modern API we recommend to use.
            You can refer to the quick gallery example and adapt the CMakeLists.txt to your needs.

            This project expects QML sources to be resource files. For the QML part to be dealt with by the IDE's code model, QML files have to be known to the IDE and be part of the build. Including AudioInfoBox.qmlin the qt_add_executabledirective takes care of the former, but not of the latter. This is why the file is not installed in the build tree. main.cpp expects it at ":/", but the only file in that resource tree is quickcontrols2.conf.

            => The quick solution is to add AudioInfoBox.qml right below quickcontrols2.conf in the qt_add_resourcesdeclaration.

            => The nicer solution is to refactor the CMakeLists.txtand use qt_add_qml_module. If you use the example as a template, there shouldn't be any errors.

            A Offline
            A Offline
            Ash V
            wrote on 8 Dec 2023, 08:27 last edited by Ash V 12 Aug 2023, 08:31
            #5

            @Axel-Spoerl
            Thank you very much for answering Axel.
            But the quick fix of adding AudioInfoBox.qml in the qt_add_resources isn't working.

            And, for the second solution you suggested, to refractor the CMakeLists.txt to use the qt_add_qml_module (via taking reference of the quick gallery example).
            In that, I'm actually not able to figure out what to provide as the URI in that module.

            The version of CMakeLists.txt I'm running is 3.21.1, and the version used in the quick gallery example is 3.16. And seriously speaking, there've been a lot of changes in the file structure.

            I request you to tell me what I should write as the URI.
            I'm attaching the screenshot here.

            0f99d5da-b421-4c5f-ba2a-ee421b9b8286-Screenshot from 2023-12-08 13-57-40.png

            The left one is my project.
            On the right side, it's the suggested project.

            A 1 Reply Last reply 8 Dec 2023, 08:48
            0
            • A Ash V
              8 Dec 2023, 08:27

              @Axel-Spoerl
              Thank you very much for answering Axel.
              But the quick fix of adding AudioInfoBox.qml in the qt_add_resources isn't working.

              And, for the second solution you suggested, to refractor the CMakeLists.txt to use the qt_add_qml_module (via taking reference of the quick gallery example).
              In that, I'm actually not able to figure out what to provide as the URI in that module.

              The version of CMakeLists.txt I'm running is 3.21.1, and the version used in the quick gallery example is 3.16. And seriously speaking, there've been a lot of changes in the file structure.

              I request you to tell me what I should write as the URI.
              I'm attaching the screenshot here.

              0f99d5da-b421-4c5f-ba2a-ee421b9b8286-Screenshot from 2023-12-08 13-57-40.png

              The left one is my project.
              On the right side, it's the suggested project.

              A Offline
              A Offline
              Axel Spoerl
              Moderators
              wrote on 8 Dec 2023, 08:48 last edited by
              #6

              @Ash-V
              The URI is the name of the app. Could be "qtTut9" in your case. You are basically free to choose.

              But rather than fixing and refactoring at the same time: Let's roll back to your initial approach, which is to add AudioInfoBox.qmlas a resource.

              Add the following to main.cpp

              => in include part
              #include <QDirIterator>

              => After QGuiApplication app(arc, argv);

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

              This will print all resource files available. Post the output here. Then we'll see how to make AudioInfoBox.qmlknown to the QML engine.

              Software Engineer
              The Qt Company, Oslo

              A 1 Reply Last reply 8 Dec 2023, 09:15
              0
              • A Axel Spoerl
                8 Dec 2023, 08:48

                @Ash-V
                The URI is the name of the app. Could be "qtTut9" in your case. You are basically free to choose.

                But rather than fixing and refactoring at the same time: Let's roll back to your initial approach, which is to add AudioInfoBox.qmlas a resource.

                Add the following to main.cpp

                => in include part
                #include <QDirIterator>

                => After QGuiApplication app(arc, argv);

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

                This will print all resource files available. Post the output here. Then we'll see how to make AudioInfoBox.qmlknown to the QML engine.

                A Offline
                A Offline
                Ash V
                wrote on 8 Dec 2023, 09:15 last edited by
                #7

                @Axel-Spoerl
                Axel, the output prints a lot of files, and AudioInfoBox.qml is also really one of those.

                b7447d21-dd55-47d7-a672-5e5626f041ed-image.png

                How should we now make AudioInfoBox.qml known to the QML engine?

                A 1 Reply Last reply 8 Dec 2023, 10:11
                0
                • A Ash V
                  8 Dec 2023, 09:15

                  @Axel-Spoerl
                  Axel, the output prints a lot of files, and AudioInfoBox.qml is also really one of those.

                  b7447d21-dd55-47d7-a672-5e5626f041ed-image.png

                  How should we now make AudioInfoBox.qml known to the QML engine?

                  A Offline
                  A Offline
                  Axel Spoerl
                  Moderators
                  wrote on 8 Dec 2023, 10:11 last edited by
                  #8

                  @Ash-V
                  The project seems messed up:

                  • There is a subdirectory called "components" in Creator's project tree. I don't see it in CMakeLists.txt.
                    Probably you have posted just a part of it.
                    That's sad, because hiding stuff confuses those, you expect help from.

                  • The "components" subdirectory is imported in main.qml.

                  • AudioInfoBox.qml is in the root resource directory (because I told you so, in the assumption you wanted to call it from main.cpp). It should be in the "components" directory, wherever that is. In that case it would be part of the import, hence known to the QML engine.

                  • main.cppadds ":/qml" as an import path. That path is nowhere in CMakeLists.txt and nowhere in the project tree. Why is it added?

                  Maybe it's time to clean the project up and do it like documented here.

                  Software Engineer
                  The Qt Company, Oslo

                  A 1 Reply Last reply 8 Dec 2023, 10:50
                  0
                  • A Axel Spoerl
                    8 Dec 2023, 10:11

                    @Ash-V
                    The project seems messed up:

                    • There is a subdirectory called "components" in Creator's project tree. I don't see it in CMakeLists.txt.
                      Probably you have posted just a part of it.
                      That's sad, because hiding stuff confuses those, you expect help from.

                    • The "components" subdirectory is imported in main.qml.

                    • AudioInfoBox.qml is in the root resource directory (because I told you so, in the assumption you wanted to call it from main.cpp). It should be in the "components" directory, wherever that is. In that case it would be part of the import, hence known to the QML engine.

                    • main.cppadds ":/qml" as an import path. That path is nowhere in CMakeLists.txt and nowhere in the project tree. Why is it added?

                    Maybe it's time to clean the project up and do it like documented here.

                    A Offline
                    A Offline
                    Ash V
                    wrote on 8 Dec 2023, 10:50 last edited by Ash V 12 Aug 2023, 10:51
                    #9

                    @Axel-Spoerl
                    Thanks for your support Axel.

                    Finally, the issue was solved. I saw the path of main.qml and added the AudioInfoBox.qml file the same way, with the same path prefix. I added the AudioInfoBox.qml file like this.

                    34ddfe2d-82bf-4203-ba88-9f6698197b05-image.png

                    Now it's working fine! 👏️

                    1 Reply Last reply
                    0

                    1/9

                    7 Dec 2023, 14:54

                    • Login

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