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. ContentItem of ListView
Forum Updated to NodeBB v4.3 + New Features

ContentItem of ListView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
15 Posts 5 Posters 2.1k 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.
  • alemioA Offline
    alemioA Offline
    alemio
    wrote on last edited by VRonin
    #1

    I have ListView with some items, but contentItem of this ListView give me only a subset of these items and it is not in order.

    This is an example

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.5
    
    Window {
        visible: true
        width: 200
        height: 100
    
        property var listModel: ListModel {
            ListElement { text: "1" }
            ListElement { text: "2" }
            ListElement { text: "3" }
            ListElement { text: "4" }
            ListElement { text: "5" }
            ListElement { text: "6" }
            ListElement { text: "7" }
            ListElement { text: "8" }
            ListElement { text: "9" }
            ListElement { text: "10" }
            ListElement { text: "11" }
            ListElement { text: "12" }
            ListElement { text: "13" }
            ListElement { text: "14" }
            ListElement { text: "15" }
            ListElement { text: "16" }
            ListElement { text: "17" }
            ListElement { text: "18" }
            ListElement { text: "19" }
            ListElement { text: "20" }
            ListElement { text: "21" }
            ListElement { text: "22" }
            ListElement { text: "23" }
            ListElement { text: "24" }
            ListElement { text: "25" }
            ListElement { text: "26" }
            ListElement { text: "27" }
            ListElement { text: "28" }
            ListElement { text: "29" }
            ListElement { text: "30" }
          }
    
        ListView{
            id:listView
            width: 100
            height: 100
            model: listModel
            delegate: Rectangle {
                id: delegateItem
                width: parent.width; height: 100
    
                Text {
                  id: itexItem
                  font.pixelSize: 40
                  text: model.text
                }
           }
        }
    
    
        Button {
            id: button
            x: 100
            y: 0
            text: qsTr("Button")
            onPressedChanged:
            {
                console.log(listView.contentItem.children.length)
    
                for(var i=0; i< listView.contentItem.children.length; i++) {
                    if(listView.contentItem.children[i].children[0])
                        console.log(listView.contentItem.children[i].children[0].text)
                }
    
            }
        }
    }
    

    Please help me

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      try:

      console.log(listView.model.count)
                  for(var i=0; i< listView.model.count; i++) {
                          console.log(listView.model.get(i).text)
                  }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • alemioA Offline
        alemioA Offline
        alemio
        wrote on last edited by
        #3

        Thank you but my problem is different, because I have some logic in the delegate.
        See this example

        Text {
        id: itexItem
        font.pixelSize: 40
        text: model.text === "10"? "Test" : model.text
        }

        So I can't use model, but I should ListView the text of delegate

        VRoninV 1 Reply Last reply
        0
        • Shrinidhi UpadhyayaS Offline
          Shrinidhi UpadhyayaS Offline
          Shrinidhi Upadhyaya
          wrote on last edited by
          #4

          Hi @alemio , your code is working fine for me

           ListView{
                   id:listView
                   width: 100
                   height: 100
                   model: listModel
                   delegate: Rectangle {
                       id: delegateItem
                       width: parent.width; height: 100
           
                       Text {
                           id: itexItem
                           font.pixelSize: 40
                           text: model.text === "10"? "Test" : model.text
                       }
                   }
               }
          

          Sample Output:-

          170de592-f9e3-4b6d-b004-b4a0454a5b25-image.png

          Shrinidhi Upadhyaya.
          Upvote the answer(s) that helped you to solve the issue.

          1 Reply Last reply
          0
          • alemioA alemio

            Thank you but my problem is different, because I have some logic in the delegate.
            See this example

            Text {
            id: itexItem
            font.pixelSize: 40
            text: model.text === "10"? "Test" : model.text
            }

            So I can't use model, but I should ListView the text of delegate

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @alemio said in ContentItem of ListView:

            because I have some logic in the delegate.

            The delegate is just for painting it should not contain data logic. You should move that logic in a proxy model

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Aikens6
              Banned
              wrote on last edited by Aikens6
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • alemioA Offline
                alemioA Offline
                alemio
                wrote on last edited by
                #7

                Thank you, but so the answer is that I can't use the contentItem to see all items in the ListView, right?

                are all items added/deleteded dinamically to the contentItem?

                1 Reply Last reply
                0
                • IntruderExcluderI Offline
                  IntruderExcluderI Offline
                  IntruderExcluder
                  wrote on last edited by
                  #8

                  @alemio said in ContentItem of ListView:

                  are all items added/deleteded dinamically to the contentItem?

                  Yes. ListView destroys items out of viewport.

                  1 Reply Last reply
                  2
                  • alemioA Offline
                    alemioA Offline
                    alemio
                    wrote on last edited by
                    #9

                    Ok, I'll explain my problem better, maybe it will be easier.
                    I would like to export a ListView to Excel. In my real project I have a a model and a proxymodel but it has more fields than the ListView and the order is different. If the way is use proxymodel, how can I do that?

                    VRoninV 1 Reply Last reply
                    0
                    • alemioA alemio

                      Ok, I'll explain my problem better, maybe it will be easier.
                      I would like to export a ListView to Excel. In my real project I have a a model and a proxymodel but it has more fields than the ListView and the order is different. If the way is use proxymodel, how can I do that?

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @alemio said in ContentItem of ListView:

                      In my real project I have a a model and a proxymodel

                      @alemio said in ContentItem of ListView:

                      how can I do that?

                      As mentioned, you should move the logic you currently have in the delegate to the proxy model and use that to save.
                      Is your model a QML model or a C++(QAbstractItemModel) one?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      2
                      • alemioA Offline
                        alemioA Offline
                        alemio
                        wrote on last edited by alemio
                        #11

                        The model is a c++ model QAbstractItemModel. I have a unique model for each table in the database (autogenerated from orm). I have also a proxymodel for these c++ model

                        1 Reply Last reply
                        0
                        • alemioA Offline
                          alemioA Offline
                          alemio
                          wrote on last edited by
                          #12

                          My proxy model is derived from SortFilterProxyModel

                          @VRonin Tell me please if I have a colored row or different font, how can I export that in excel?

                          1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            You can use this library to create the excel file with custom formatting.

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            alemioA 1 Reply Last reply
                            0
                            • VRoninV VRonin

                              You can use this library to create the excel file with custom formatting.

                              alemioA Offline
                              alemioA Offline
                              alemio
                              wrote on last edited by
                              #14

                              @VRonin I've already taken this library.
                              I just need to understand how put some logic in proxy model as you told me.
                              I saw DelegateModel component, but I don't find example how can I extract data in c++ (I think at the end it is the same problem like listview delegate)

                              VRoninV 1 Reply Last reply
                              0
                              • alemioA alemio

                                @VRonin I've already taken this library.
                                I just need to understand how put some logic in proxy model as you told me.
                                I saw DelegateModel component, but I don't find example how can I extract data in c++ (I think at the end it is the same problem like listview delegate)

                                VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #15

                                @alemio said in ContentItem of ListView:

                                I just need to understand how put some logic in proxy model

                                It's not really clear what exactly you are doing but I suspect you'd want to override the data() method of the proxy to convert some value in the base model to a different value in the proxy

                                but I don't find example how can I extract data in c++

                                Check out JsonModelSerialiserPrivate::toJsonObject of this file for an example. this extracts the data and saves it in a JSON object, you basically have to do the same but save it in an excel file

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                1 Reply Last reply
                                2

                                • Login

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