QML check the type of a property at runtime

  Kiến thức lập trình

I am trying to check the type of a property at runtime:

Node {
    id: root

    property string foo
    property color diffuseColor
    ...
}

(because I am setting it dynamically from a JS object like {foo: "bar", diffuseColor: [0.5, 1, 0]} only that colors are given in an incompatible format, so a simple assignment would not work.

I need code like:

var targetObject = ... // the QML object
for(var k in jsobj) {
    if(isColorProperty(targetObject[k]))
        targetObject[k] = Qt.rgba(jsobj[k][0], jsobj[k][1], jsobj[k][2], 1)
    else
        targetObject[k] = jsobj[k]
}

to implement that isColorProperty() I tried many different approaches, such as using instanceof (type error), checking Object.getPrototypeOf result (type error), using .toString() (only returns an HTML color string) none of those worked.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT