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. How to refer to a QML file component?

How to refer to a QML file component?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 607 Views
  • 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 Offline
    P Offline
    pderocco
    wrote on last edited by
    #1

    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-worxR 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Not sure but this should work:

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

      (Z(:^

      P 1 Reply Last reply
      1
      • P pderocco

        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-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @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
        2
        • sierdzioS sierdzio

          Not sure but this should work:

          Component {
            id: myTab
            YourQmlFileName {
            }
          }
          
          // in tab view:
          addTab("Title", myTab)
          
          P Offline
          P Offline
          pderocco
          wrote on last edited by
          #4

          @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
          1

          • Login

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