Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling method from modelData
Forum Updated to NodeBB v4.3 + New Features

Calling method from modelData

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 407 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pantoufle
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      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
      

      C++ is a perfectly valid school of magic.

      P 1 Reply Last reply
      2
      • fcarneyF fcarney

        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
        
        P Offline
        P Offline
        Pantoufle
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          the string inside qml is not a QString object, but IIRC a JS string, that one does not have the contains function


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          4
          • P Offline
            P Offline
            Pantoufle
            wrote on last edited by
            #5

            Thank you for helping me understand.

            Have a nice day

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved