在将QtQuick / QML对象传递给javascript函数后访问其属性



仍未解决虽然我的错误修复适用于文本项,但它不适用于鼠标区域。console.log(key.keyMouseArea)结果为"未定义"。但是我可以使用key.children[1]!?!?!访问它

部分解决:我的错,因为text是一个孩子的财产,我可以使用key.textField.text访问它。

我使用this指针将一个 QtQuick 对象传递给另一个 QtQuick 对象的 javascript 函数。在调用的函数中,我可以看到,在日志输出中,传递成功(VirtualKey_QMLTYPE_0(0x14a6bf0((。现在,我希望访问此对象的属性,但该对象是匿名的,或者大约是匿名的。如何将此参数转换或转换为其原始类型?

类似Qt.castToQtQuickObject(pointer, VirtualKey)或类似的东西。

可用的createComponentcreateObject方法不是我需要的,因为它们会创建新实例。

一些文件.qml:

Controller {
id: controller
}
VirtualKey {
Text { // didn't think of this child when asking ...
id: textField
text: "Hallo"
}
MouseArea {
objectName: "mouse"
id: keyMouseArea
}
Component.onCompleted: {
controller.registerKey(this) // passing this instance of VirtualKey
}
}

Controller.qml:

QtObject {
function registerKey(key) {
console.log("VKC: registering key " + key)
// Output: VirtualKey_QMLTYPE_0(0x14a6bf0)
console.log(key.textField) // works
console.log(key.keyMouseArea) // undefined !!!
console.log(key.children[1]) // works
console.log(key.children[1].objectName) // -> "mouse"
}
}

出于某种原因,我在VirtualKey中有一个文本项的属性别名。这就是它适用于textField而不是keyMouseArea的原因。

在VirtualKey中添加property alias keyMouseArea: keyMouseArea为我解决了这个问题。现在我也可以使用key.keyMouseArea来访问鼠标区域。

最新更新