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. Can't change properties of ListElement with setProperty methode

Can't change properties of ListElement with setProperty methode

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 756 Views
  • 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.
  • L Offline
    L Offline
    Luffy97
    wrote on last edited by Luffy97
    #1

    Hello everyone,

    I was working on my drawer menu for an andriod app and filled the menupoint with using ListView. I want to hightlight the chosen ListElement permanently, until the user goes to the menu again and choses to click another ListElement. In that case the old highlighted ListElement should go back to orignal color and the newly clicked on to the highlighted color.

    The problem is that I cannot change the properties of non-current index ListElements.

    Let's say I have a basiccode like:

    ListModel{
     id:model
    
    ListElement{itemcolor:"red"}
    ListElement{itemcolor:"red"}
    
    //Listview and other code...
    
    MouseArea {
    anchors.fill: parent
    onClicked: {
    model.currentIndex = index
    model.setProperty(1,"itemcolor","blue")
    }
    }
    
    

    So in this shorten code to illustrate my problem, when I click the second ListElement, I would expect the first one to go blue, but it instead gives me this error in the console:

    TypeError: Property 'setProperty' of object QQmlDMAbstractItemModelData(0x78c9dbf180) is not a function

    However I even checked in the documentation of ListModel QML Type (https://doc.qt.io/qt-6/qml-qtqml-models-listmodel.html) and see there, setProperty function is defined in the same way as I use it. So why can't I change the properties of other ListElements by clicking one specific Listelement?

    I'm using Qt6.5.

    Thanks in advance for your support.

    GrecKoG 1 Reply Last reply
    0
    • L Luffy97

      Hello everyone,

      I was working on my drawer menu for an andriod app and filled the menupoint with using ListView. I want to hightlight the chosen ListElement permanently, until the user goes to the menu again and choses to click another ListElement. In that case the old highlighted ListElement should go back to orignal color and the newly clicked on to the highlighted color.

      The problem is that I cannot change the properties of non-current index ListElements.

      Let's say I have a basiccode like:

      ListModel{
       id:model
      
      ListElement{itemcolor:"red"}
      ListElement{itemcolor:"red"}
      
      //Listview and other code...
      
      MouseArea {
      anchors.fill: parent
      onClicked: {
      model.currentIndex = index
      model.setProperty(1,"itemcolor","blue")
      }
      }
      
      

      So in this shorten code to illustrate my problem, when I click the second ListElement, I would expect the first one to go blue, but it instead gives me this error in the console:

      TypeError: Property 'setProperty' of object QQmlDMAbstractItemModelData(0x78c9dbf180) is not a function

      However I even checked in the documentation of ListModel QML Type (https://doc.qt.io/qt-6/qml-qtqml-models-listmodel.html) and see there, setProperty function is defined in the same way as I use it. So why can't I change the properties of other ListElements by clicking one specific Listelement?

      I'm using Qt6.5.

      Thanks in advance for your support.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @Luffy97 that's because you are in a delegate and model here refers to a context property exposing data for the current row. It shadows the id of your ListModel. Use another id and you should be fine.

      Note that I wouldn't store the selected state in the model. Doing it in the view is easier usually.

      L 1 Reply Last reply
      0
      • GrecKoG GrecKo

        @Luffy97 that's because you are in a delegate and model here refers to a context property exposing data for the current row. It shadows the id of your ListModel. Use another id and you should be fine.

        Note that I wouldn't store the selected state in the model. Doing it in the view is easier usually.

        L Offline
        L Offline
        Luffy97
        wrote on last edited by
        #3

        @GrecKo Thanks for the info. It works now! I will keep that advice in mind, thanks alot.

        Best regards

        1 Reply Last reply
        0
        • L Luffy97 has marked this topic as solved on
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4
          This post is deleted!
          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