gpt4 book ai didi

qml - 从 C++ 访问动态 QML 对象

转载 作者:行者123 更新时间:2023-12-02 02:25:40 27 4
gpt4 key购买 nike

有人知道如何从 C++ 访问和存储动态创建的 QML 对象吗?我使用 Qt 站点上建议的以下代码来创建动态 QML 对象并尝试将它们存储在 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)
}
}

谢谢。

简历

最佳答案

有关于此的文档。 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() 会起作用,也可能不会。你必须尝试一下。

关于qml - 从 C++ 访问动态 QML 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6104199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com