How to iterate for each element of collection



  • Hello,
    I got some trouble with my collection of displayed elements.

    The code is look like:

    @ ListModel {
    id: boardModel
    }

      GridView{
         id: myGridView
         anchors.fill: myBoard
         model: boardModel
         cellHeight: myBoard.frameSize
         cellWidth: myBoard.frameSize
         delegate: SingleTile{}
        }
      function flipFrames(which){
    
          for(var index=0;index<myGridView.count;++index){
              myGridView.currentIndex = index
              myGridView.currentItem.setFlipped(which)
          }
          myGridView.currentIndex = which
      } @
    

    In function flipFrames after clicking on one element form the listModel I execute function from SingleTile object for each element. This solution has some issue because when Im changing currentIndex the window sometims start to move.

    Does anybody knows how to do something like that without changing currentIndex value ?



  • Hi

    I would probably do something like
    @
    GridView{
    id: myGridView
    anchors.fill: myBoard
    model: boardModel
    cellHeight: myBoard.frameSize
    cellWidth: myBoard.frameSize
    property int which : -1 //new
    delegate: SingleTile{
    id : theDelegate
    Connections{ //new
    target : myGridView
    onWhichChanged :{
    theDelegate.setFlipped(myGridView.which);
    }
    }
    }
    }

    function flipFrames(which){
    myGridView.which = which;
    }
    @

    The idea (which I find useful with QML) is to use property bindings and let each element act individually. This is a bit different from c++ where we are used to looping over a collection and tell each element what to do.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.