Calling method from modelData
-
Hello,
I'm currently displaying a QStringList via a ListView like this (SvgIconLoader is a custom component ) :
ListView { spacing: 5 anchors.fill: parent anchors.top: parent.top anchors.topMargin: 25 model: mygui.getMyList // A QStringList delegate: RowLayout { anchors.left: parent.left anchors.leftMargin: 25 spacing: 20 SvgIconLoader { id: myIcon icon : "qrc:/Ressources/images/test.svg" size: 40 } Text { text: modelData font.pixelSize: 24 color: "white" } } }
This code works perfectly. Now I'm trying to add a feature, I would like when the QString contained in "modelData" contains for example the letter "E", then it changes the color of the text to green.
So I tried this:
color: modelData.contains("E") ? "green" : "white"
But it doesn't seems to work.
My question is, can we call QObject method from a modelData? If not, is there a solution to this problem? -
What you have should be working.Are you getting errors in the Application Output window?Edit: Hmmm, in a simple test contains is not working.
"TypeError: Property 'contains' of object Erp is not a function"Edit: Use indexOf instead (cause javascript I guess, not all QString stuff is supported):
property var vstr: "Erp" property bool vbool: "%1".arg(vstr).indexOf("E") !== -1
-
What you have should be working.Are you getting errors in the Application Output window?Edit: Hmmm, in a simple test contains is not working.
"TypeError: Property 'contains' of object Erp is not a function"Edit: Use indexOf instead (cause javascript I guess, not all QString stuff is supported):
property var vstr: "Erp" property bool vbool: "%1".arg(vstr).indexOf("E") !== -1
@fcarney said in Calling method from modelData:
What you have should be working.Are you getting errors in the Application Output window?Edit: Hmmm, in a simple test contains is not working.
"TypeError: Property 'contains' of object Erp is not a function"Edit: Use indexOf instead (cause javascript I guess, not all QString stuff is supported):
property var vstr: "Erp" property bool vbool: "%1".arg(vstr).indexOf("E") !== -1
it works, thanks a lot !
On the other hand, I find it odd that we are only entitled to certain methods, we have to guess them?
-
the string inside qml is not a QString object, but IIRC a JS string, that one does not have the contains function