Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 6.5: accessing QML file from C++
Forum Updated to NodeBB v4.3 + New Features

Qt 6.5: accessing QML file from C++

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 956 Views 3 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 6 Apr 2023, 16:43 last edited by
    #1

    Hi all -

    I'm in the process of converting my application to 6.5, and taking advantage of the new QML module features as described here. As part of the conversion, I stopped using a qml.qrc file. My files are all defined in my CMakeLists.txt file:

    qt_add_qml_module(appnga_demo
        URI nga_demo
        VERSION 1.0
        QML_FILES
        Main.qml
        Activitydrawer.qml
        Auxtabbar.qml
        ...
    

    I can access all of my QML objects from my qml files by importing nga_demo. But my C++ code (of course) doesn't "see" my qml files:

        QUrl("qrc:/Pump.qml") // <== obviously doesn't work
    

    So, do I need my qml.qrc file after all, or is there a new way to reference the files in my module?

    Thanks...

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jeremy_k
      wrote on 9 Apr 2023, 05:35 last edited by
      #5

      From a high level, the module API looks like a nice thing to use. When I've directly instantiated QML from C++, it's only ever been to load a single top level Item. As @mzimmers suggests, I've found it better to keep the presentation layer pretty much to itself. The interaction that is necessary should be via signals and slots, or events and event handlers.

      A C++ slot might be the right place to instantiate an instance of a QQuickComponent provided by a QML caller. This is how containers such as Repeater and ListModel work.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jeremy_k
        wrote on 8 Apr 2023, 08:23 last edited by jeremy_k 4 Aug 2023, 08:24
        #2

        I haven't used the facility yet, but this appears to be answered in the same document:

        The line of interest is const QUrl url(u"qrc:/qt/qml/helloqml/main.qml"_s);. There are multiple gotchas in here. The first is that because we are dealing with a QML module, main.qml has now been placed under a helloqml folder[...]

        Asking a question about code? http://eel.is/iso-c++/testcase/

        M 1 Reply Last reply 8 Apr 2023, 12:42
        0
        • J jeremy_k
          8 Apr 2023, 08:23

          I haven't used the facility yet, but this appears to be answered in the same document:

          The line of interest is const QUrl url(u"qrc:/qt/qml/helloqml/main.qml"_s);. There are multiple gotchas in here. The first is that because we are dealing with a QML module, main.qml has now been placed under a helloqml folder[...]

          M Offline
          M Offline
          mzimmers
          wrote on 8 Apr 2023, 12:42 last edited by
          #3

          @jeremy_k I guess what I'm wondering is whether the recommended procedure for exposing QML files to C++ is still the qml.qrc file, or if there's a newer, better way to do it.

          After thinking about this problem, it occurs to me that in this particular example, I probably shouldn't be referencing the file in C++ anyway, as this information is better kept in the QML side of the house, but I'm still interested in the preferred way to go about it.

          A 1 Reply Last reply 8 Apr 2023, 13:59
          0
          • M mzimmers
            8 Apr 2023, 12:42

            @jeremy_k I guess what I'm wondering is whether the recommended procedure for exposing QML files to C++ is still the qml.qrc file, or if there's a newer, better way to do it.

            After thinking about this problem, it occurs to me that in this particular example, I probably shouldn't be referencing the file in C++ anyway, as this information is better kept in the QML side of the house, but I'm still interested in the preferred way to go about it.

            A Offline
            A Offline
            Asperamanca
            wrote on 8 Apr 2023, 13:59 last edited by
            #4

            @mzimmers
            There is access via URI and typename e.g. in https://doc.qt.io/qt-6/qqmlcomponent.html#QQmlComponent-6
            In general, I recommend reading this: https://www.qt.io/blog/whats-new-for-qml-modules-in-6.5

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jeremy_k
              wrote on 9 Apr 2023, 05:35 last edited by
              #5

              From a high level, the module API looks like a nice thing to use. When I've directly instantiated QML from C++, it's only ever been to load a single top level Item. As @mzimmers suggests, I've found it better to keep the presentation layer pretty much to itself. The interaction that is necessary should be via signals and slots, or events and event handlers.

              A C++ slot might be the right place to instantiate an instance of a QQuickComponent provided by a QML caller. This is how containers such as Repeater and ListModel work.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              1
              • M mzimmers has marked this topic as solved on 13 Jul 2023, 18:24

              1/5

              6 Apr 2023, 16:43

              • Login

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