从 C++ 访问动态 QML 对象

发布于 2024-11-09 21:32:35 字数 841 浏览 0 评论 0原文

有谁知道如何访问和存储从 C++ 动态创建的 QML 对象? 我使用 Qt 网站上建议的以下代码来创建动态 QML 对象并尝试将它们存储在 QML 列表类型

    property list<Button> listButtons: [
        Button{ }
    ]
    function addButton(buttonname) {
        console.log("Creating Pin: "+buttonname)
        var component = Qt.createComponent("Button.qml");
        if (component.status == Component.Ready)
        {
            var newbutton = component.createObject(node);
            newbutton.x = 20;
            newbutton.y = 30;
            listButtons.append(newbutton) //I get a error here: listButtons.append [undefined] is not a function
        }
        else
        {
            console.log("Unable to create button: "+buttonname)
        }
     }

谢谢。

简历

Is anyone aware of how to access and store dynamically created QML objects from C++?
I used the following code suggested on Qt Site for creating dynamic QML objects and trying to store them in a QML list type

    property list<Button> listButtons: [
        Button{ }
    ]
    function addButton(buttonname) {
        console.log("Creating Pin: "+buttonname)
        var component = Qt.createComponent("Button.qml");
        if (component.status == Component.Ready)
        {
            var newbutton = component.createObject(node);
            newbutton.x = 20;
            newbutton.y = 30;
            listButtons.append(newbutton) //I get a error here: listButtons.append [undefined] is not a function
        }
        else
        {
            console.log("Unable to create button: "+buttonname)
        }
     }

Thank you.

CV

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残月升风 2024-11-16 21:32:35

有关于此的文档。 http://doc.qt.nokia.com/4.7/qml-list.html

要实现此目的,您需要

import QtQuick 1.0
import "script.js" as JsScript

Rectangle {
    width: 360
    height: 360

    function getList(){
        return JsScript.array;
    }

    Text {
        anchors.centerIn: parent
        text: "Hello World"
    }
    Item {
     Component.onCompleted: {
         console.log('complemented');
         JsScript.addItem('abc')
         console.log("Added:", JsScript.array[0])
     }
    }
}

未经测试的代码中将数组实现为列表script.js

var array = new Array();

function  getArray(){
    return array;
}
    function addItem(item) {
     array.push(item)
    }

从 c++

QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();

QVariant returnedValue;
QVariant msg = "Hello from C++";
QMetaObject::invokeMethod(object, "myQmlFunction",
     Q_RETURN_ARG(QVariant, returnedValue),
     Q_ARG(QVariant, msg));

returnedValue.toList();


嗯,我对此不确定。但也许 QVariant.toList() 会起作用,也可能不会。你必须尝试一下。

There is documentation regarding this. http://doc.qt.nokia.com/4.7/qml-list.html

To achieve this you need to implement an array as a list

import QtQuick 1.0
import "script.js" as JsScript

Rectangle {
    width: 360
    height: 360

    function getList(){
        return JsScript.array;
    }

    Text {
        anchors.centerIn: parent
        text: "Hello World"
    }
    Item {
     Component.onCompleted: {
         console.log('complemented');
         JsScript.addItem('abc')
         console.log("Added:", JsScript.array[0])
     }
    }
}

script.js

var array = new Array();

function  getArray(){
    return array;
}
    function addItem(item) {
     array.push(item)
    }

from c++

QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();

QVariant returnedValue;
QVariant msg = "Hello from C++";
QMetaObject::invokeMethod(object, "myQmlFunction",
     Q_RETURN_ARG(QVariant, returnedValue),
     Q_ARG(QVariant, msg));

returnedValue.toList();

Untested code.
hmmm, i am not sure about this.But maybe QVariant.toList() will work or maybe it wont.You'll have to try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文