Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

i need to change the order of row elements in gridview



  • Hi All,
    I'm using a gridview in QML which is being populated by a QList.

    Currently I have it displayed 4 items per row in QML in following order.

    [ITEM1] [ITEM2] [Item3] [item4]
    [ITEM5] [ITEM6] [Item7] [item8]
    [ITEM9] [ITEM10] [Item11] [item12]

    Now I wanted to populate the Items order in following way.

    [ITEM1] [ITEM2] [Item3] [item4]
    [ITEM8] [ITEM7] [Item6] [item5]
    [ITEM9] [ITEM10] [Item11] [item12]

    any ways complete my requirement by using Grid View or
    please suggest any other methods to fulfill my requirement.

    Thanks.



  • @sainaresh
    The GridView fills the elements in row-major fashion, i.e. by row, so make sure your QList fills the data in the order expected by the view.
    If you can't change the element order of the QList then you need to use an index lookup array to map QList indices to GridView indices.

    GridView {
          property var gridIdx = [0,1,2,3,7,6,5,4,8,9,10,11]
           model: Array.from(mylist)
           delegate: Text {
               text: mylist[gridIdx[index]]
          }
    }
    

    Or something like that. I remember reading somewhere that it is discouraged to use the index property within a delegate, and so normally you would use modelData here instead of mylist[index]...



  • @Diracsbracket said in i need to change the order of row elements in gridview:

    I remember reading somewhere that it is discouraged to use the index property within a delegate

    This makes no sense to me. modelData is not available in every delegate. It depends upon the model data sources. But index is always available.



  • @fcarney said in i need to change the order of row elements in gridview:

    modelData is not available in every delegate

    If you mean every model, then sure (or do you mean delegates with required properties that are not matched by model properties?). For models that do provide the modelData role though, it makes some sense.



  • I would say don't do this with index:

    property var list: [...]
    
    ListView {
      model: list
      delegate: Item {
        list[index]
      }
    }
    

    So now I get what you mean. Yes, in that case modelData makes sense.


Log in to reply