Dynamically load/assign objects in QML

时间:2015-11-12 10:59:37

标签: qml

I've just started with QML game development using V-Play and coming from java, I stil have problems understanding some basics.

I know how I'd do it in Java, but don't know how it works in QML, so I'll explain it in Java terms:

I have a "base class" with some properties and methods. Then I have modules extending this base class with more specific features. (Like "class Module1 extends BaseModule")

Then I have an object ("ModuleContainer") that contains a Module, but I don't know which one - it's loaded dynamically at runtime. Thus, in Java i'd create a new object like "BaseModule someModule = new Module1()", and can later access it and also replace it (like "someModule = new Module2()").

How could I do that in QML?

I've tried properties, but I haven't found a way to create a new object and then use it. Dynamic creation of objects with an entityManager doesn't quit seem to work for this either.

1 个答案:

答案 0 :(得分:1)

我和组件一起使用组件,它与装载机一起玩,它几乎就是我想要的。

如果其他人需要这样的话,我的代码就在这里:

基本上,我创建了一个基类(BaseModule),创建了两个扩展该类的模块(java术语)。

在一个新类(ModuleSlot)中,我创建了两个包含每个模块的组件,可以在主代码中动态加载和替换。

重要部分

//define a component and make it accessible from the outside
property Component cmodule1: module1   
Component {
   id: module1
   Module1 {
   }
}

//define a component to hold the component to use (for easier changing)   
property Component dynamicModule: dynamicModuleHolder.cmodule1 

ModuleSlot {
   id:dynamicModuleHolder
}  

//the magic happens here: the defined component is loaded dynamically on runtime, 
when changed, the old one is removed and the new one loaded

Loader {
   sourceComponent:dynamicModule  
}

完整代码

<强> Main.qml:

GameWindow {
id: gameWindow

//licenseKey: "<generate one from http://v-play.net/licenseKey>"

activeScene: scene

width: 640
height: 960


property Component dynamicModule: dynamicModuleHolder.cmodule1
property int activeElement: 1

Scene {


    id: scene

    width: 640
    height: 960



    Loader{
        sourceComponent:dynamicModule
    }

    ModuleSlot {
        id:dynamicModuleHolder
    }

    MouseArea {
           anchors.fill: parent
           onClicked: {
               if(activeElement == 1) {
                   dynamicModule = dynamicModuleHolder.cmodule2
                   activeElement = 2
               } else {
                   dynamicModule = dynamicModuleHolder.cmodule1
                   activeElement = 1
               }
           }
       }



}

}

<强> BaseModule.qml:

Item {
       property int someAttribute: 0
}

Module1.qml / Module2.qml (Module2只有不同的矩形)

BaseModule {

    someAttribute: 5 // just to show that Module1 inherits from BaseModule

    Rectangle {
        color: "red"
        width: 50
        height: 50
        x: 200
        y: 200
    }
}

<强> ModuleSlot

Item {

    property Component cmodule1: module1
    property Component cmodule2: module2

    QtObject {
        id: internalSettings
        property color color: "green"
    }

    Component {
        id: module1
        Module1 {

        }
    }

    Component {
        id: module2
        Module2 {

        }
    }
}