gpt4 book ai didi

javascript - 如何在 QML 中执行 "is_a"、 "typeof"或 instanceof ?

转载 作者:IT王子 更新时间:2023-10-29 03:23:15 26 4
gpt4 key购买 nike

我想遍历 QML 组件列表并选择一种类型:

for (var i = 0; i < controls.children.length; ++i) {
if ( typeof (controls.children[i].height) == "QDeclarativeRectangle")
{
// do stuff
}
}

如何做到这一点?

最佳答案

从 Qt 5.10 开始,您终于可以使用 instanceOf 来检查变量是否属于某种 QML 类型,参见 "QML Support for Enum and InstanceOf Type Checks" .

import VPlayApps 1.0
import QtQuick 2.0

App {
// two QML items, used for type checking
Item { id: testItem }
Rectangle { id: testRect }

// function to check wheter an item is a Rectangle
function isRectangle(item) {
return item instanceof Rectangle
}

// type check example
Component.onCompleted: {
console.log("testItem is Rectangle? " + isRectangle(testItem))
console.log("testRect is Rectangle? " + isRectangle(testRect))
}
}

关于javascript - 如何在 QML 中执行 "is_a"、 "typeof"或 instanceof ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13923794/

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