Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] QML component inheritance - problem
Qt 6.12 LTS is out! See what's new in the release blog

[SOLVED] QML component inheritance - problem

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 3.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi. I have two files:

    @
    // MainWindow.qml

    import QtQuick 2.1
    import Qt3D 2.0

    Rectangle {
    //some code
    Viewport {
    // some 3D code
    }
    }

    // Robot.qml
    import QtQuick 2.1
    import Qt3D 2.0

    MainWindow {
    Item3D {
    // some 3D robot drawing code
    }

    }

    @

    And my problem is that this isn't working. I know why, because Item3D is on the first level in MainWindow class. Item3D is child of Rectangle in MainWindow class, but Item3D must be child of Viewport in MainWindow class. How to tell the QML to put item3D in Viewport in main Window, not in the top level Rectangle ?

    1 Reply Last reply
    0
    • U Offline
      U Offline
      unai_i
      wrote on last edited by
      #2

      Hi,
      First of all, is your top level rectangle necessary to your app? If not, just make Viewport top level. Else, you could still make Viewport top level and move your rectangle to Robot.qml

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onek24
        wrote on last edited by
        #3

        @_unai_i_ : There is some code between the top-level Rectangle and the Viewport so i think it is necessary.

        @_sibislaw2_: You want to load your custom Item3D component in your MainWindow.qml file or did i misunderstood you?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unai_i
          wrote on last edited by
          #4

          @onek24: You are right I didn't notice the "some code" mention. However it is still possible to move the rectangle and the outside code to Robot.qml

          @sibislaw2: Is something like below possible in your use case?
          @
          // MainWindow.qml

          import QtQuick 2.1
           
          Rectangle {
          //some code
          MyViewport {
          }
          }
           
          // MyViewport.qml
          import QtQuick 2.1
          import Qt3D 2.0
           
          Viewport {
          // some 3D code
          Robot{
          }
          }
          
          // Robot.qml
          import QtQuick 2.1
          import Qt3D 2.0
           
          Item3D {
           // some 3D robot drawing code
          }
          

          @

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I resolved problem by using loader class.

            @
            Rectangle {
            // code
            Viewport {
            // code

                Loader {
                    id: robotLoader
                    objectName: "robotLoader"
            
                    property string robotQmlFile: ""
            
                    source: robotLoader.robotQmlFile
                }
            

            }
            }

            @

            By this code i can load dynamically my class into desired Viewport.

            1 Reply Last reply
            0

            • Login

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