0

Is there any way to get item class in QML? I know about objectName and id etc. but it looks a bit strange and not so comfortable. This is what i need:

ComboBox {
    id: comboBox
}
TextField {
    id: textField
}

function getValue(item) {
    switch(item.???) {   //what property can I use here?
        case 'ComboBox':
            return item.model.get(item.currentIndex).value;
        case 'TextField':
            return item.text;
    }
}

using objectName as described in lots of articles over the Internet might be ambiguous and excessive, for example:

ComboBox {
    id: comboBox
    objectName: "ComboBox"
}
TextField {
    id: textField
    objectName: "ComboBox" /// oops!!
}

function getValue(item) {
    switch(item.objectName) {  
        case 'ComboBox':
            return item.model.get(item.currentIndex).value;
        case 'TextField':
            return item.text;
    }
}
folibis
  • 12,048
  • 6
  • 54
  • 97
  • It's very likely that this kind of information is erased after parsing because these names are only names of files where components are declared.... – Kakadu Jun 18 '14 at 08:04

2 Answers2

1

No, this can only be done from C++.

Mitch
  • 23,716
  • 9
  • 83
  • 122
0

I'm also stucked on the same problem. The following reply could be a good workaround: https://stackoverflow.com/a/13928333/1994341.

That is to say: for each item you set the property "objectName" with a unique name. objectName is the only property of QtObject, which is inherited by each QML item.

According to me this is a miss, however QML is a great tool.

Community
  • 1
  • 1
jaddawyn
  • 351
  • 3
  • 8