9

ボタンが押されたときにその場でコンポーネントを作成し、それを現在の親に追加しようとしています。ここで何が間違っているのかわかりませんが、

私はこの単純なレイアウトを持っています:

import QtQuick 2.0
import Ubuntu.Components 0.1
import "components"
import "componentCreation.js" as MyScript

/*!
    \brief MainView with a Label and Button elements.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "com.ubuntu.developer..SpritePractice"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    width: units.gu(100)
    height: units.gu(75)

    Page {
        title: i18n.tr("Simple")

        Column {
            spacing: units.gu(1)
            anchors {
                margins: units.gu(2)
                fill: parent
            }

            Button
            {
                text: i18n.tr("Hello World!!");
                onClicked:
                {
                    var component;
                    var sprite;
                    component = Qt.createComponent("Sprite.qml");
                    sprite = component.createObject(parent, {"x": 100, "y": 100});
                }
            }
        }
    }
}

追加しようとしている「スプライト」は次のとおりです。

import QtQuick 2.0

Rectangle { width: 80; height: 50; color: "red" }

作成中のコンポーネントを現在の親に追加するにはどうすればよいですか?

解決方法:

以下の回答を使用し、Ubuntuのドキュメントを使用しました。

4

1 に答える 1

18

親の代わりに、ここで ID を指定する必要があります。

sprite = component.createObject(parent, {"x": 100, "y": 100});

フォローしてみて、

Page {
        ...

        Column {
            id: container                
            ...
            Button
            {
                text: i18n.tr("Hello World!!");
                onClicked:
                {
                    var component;
                    var sprite;
                    component = Qt.createComponent("Sprite.qml");
                    sprite = component.createObject(container, {"x": 100, "y": 100});
                }
            }
        }
    }

サンプルコードも作りましたので、そちらもご覧ください。

于 2013-10-23T05:53:38.863 に答える