[SOLVED] QML component inheritance - problem



  • 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 ?



  • 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



  • @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?



  • @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
    }
    

    @



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.