How to do a “is_a”, “typeof” or instanceof in QML?

后端 未结 4 1871
南旧
南旧 2020-12-23 21:00

I want to run through a list of QML components and choose one type:

for (var i = 0; i < controls.children.length; ++i) {
     if ( typeof (controls.childr         


        
相关标签:
4条回答
  • 2020-12-23 21:52

    Yes, this thread is 2 yrs old but perhaps my answer could help someone out there.

    For me, it was good enough use JSON.stringify() to compare QML elements. Of course, if two different elements have exactly the same properties and values, this would give you false positive. (eg when one element is on top of another with same color, x, y, etc)

    toString() did not work for me since I've created many instances from same base component. Setting objectName to every instance would have been a bit too much for my use case.

    0 讨论(0)
  • 2020-12-23 22:02

    Since Qt 5.10, you can finally use instanceOf to check whether a variable is of a certain QML type, see "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))
      }
    }
    
    0 讨论(0)
  • 2020-12-23 22:03

    You can't use typeof for this directly because it will always return you 'object' as a type of any QML element. There are several alternatives however that you could use. One is setting the objectName of the each element to its type and check that in your loop or define a property and check for that property. This will require a bit more work but you could create your qml element that has this property and than use it wherever you need it. Here is a sample code:

    Rectangle {
      id: main
      width: 300; height: 400
    
      Rectangle {
        id: testRect
        objectName: "rect"
        property int typeId: 1
      }
    
      Item {
        id: testItem
        objectName: "other"
      }
    
      Component.onCompleted: {
        for(var i = 0; i < main.children.length; ++i)
        {
            if(main.children[i].objectName === "rect")
            {
                console.log("got one rect")
            }
            else
            {
                console.log("non rect")
            }
        }
        for(i = 0; i < main.children.length; ++i)
        {
            if(main.children[i].typeId === 1)
            {
                console.log("got one rect")
            }
            else
            {
                console.log("non rect")
            }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-23 22:07

    Here's another approach using toString() (which might not be portable to the future version of QML):

    function qmltypeof(obj, className) { // QtObject, string -> bool
      // className plus "(" is the class instance without modification
      // className plus "_QML" is the class instance with user-defined properties
      var str = obj.toString();
      return str.indexOf(className + "(") == 0 || str.indexOf(className + "_QML") == 0;
    }
    
    ...
    
    for (var i = 0; i < controls.children.length; ++i) {
       if (qmltypeof(controls.children[i].height, "QDeclarativeRectangle"))
       {
         // do stuff
       }
    }
    
    0 讨论(0)
提交回复
热议问题