Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to refer to a QML file component?

    QML and Qt Quick
    3
    4
    404
    Loading More Posts
    • 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.
    • P
      pderocco last edited by

      The TabView type has an addTab method whose second argument is a component type. If I define a component inline, using the Component type, I can give it an id, and pass the id to addTab. That's what's shown in the documentation examples.

      But what if the component is defined by a QML file? How do I pass it to addTab? Using the name of the file without the .qml suffix doesn't bother the compiler, but at run-time it complains ".../TabView.qml:143: Error: Cannot assign null to QQmlComponent*".

      raven-worx 1 Reply Last reply Reply Quote 0
      • sierdzio
        sierdzio Moderators last edited by

        Not sure but this should work:

        Component {
          id: myTab
          YourQmlFileName {
          }
        }
        
        // in tab view:
        addTab("Title", myTab)
        

        (Z(:^

        P 1 Reply Last reply Reply Quote 1
        • raven-worx
          raven-worx Moderators @pderocco last edited by raven-worx

          @pderocco said in How to refer to a QML file component?:

          But what if the component is defined by a QML file? How do I pass it to addTab?

          see this

          var comp = Qt.createComponent("MyComponent.qml");
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 2
          • P
            pderocco @sierdzio last edited by

            @sierdzio @raven-worx Thanks. Both methods seem to work.

            I assumed that Component created another object around what was generated by the QML file, and would require property aliases to bridge between the two, but that's not the case.

            As it turns out, though, using Component is more appropriate in my case, because it provides an opportunity to customize it. My component is a simple HTML help viewer, which handles links internally, but emits a newTab signal when a link is long-pressed, so I can write:

            Component {
                id: htmlPage
                HtmlPage { onNewTab: createNewTab(url) }
            }
            

            rather than build the createNewTab call into the HtmlPage object.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post