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?
Forum Updated to NodeBB v4.3 + New Features

How to refer to a QML file component?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 613 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.
  • P Offline
    P Offline
    pderocco
    wrote on 19 Feb 2019, 06:51 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*".

    R 1 Reply Last reply 19 Feb 2019, 12:01
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 19 Feb 2019, 07:15 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 20 Feb 2019, 08:40
      1
      • P pderocco
        19 Feb 2019, 06:51

        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*".

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 19 Feb 2019, 12:01 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
        • S sierdzio
          19 Feb 2019, 07:15

          Not sure but this should work:

          Component {
            id: myTab
            YourQmlFileName {
            }
          }
          
          // in tab view:
          addTab("Title", myTab)
          
          P Offline
          P Offline
          pderocco
          wrote on 20 Feb 2019, 08:40 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

          1/4

          19 Feb 2019, 06:51

          • Login

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