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. ListView showing a constant number of items despite of it's height
QtWS25 Last Chance

ListView showing a constant number of items despite of it's height

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.0k 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.
  • DuBuD Offline
    DuBuD Offline
    DuBu
    wrote on last edited by
    #1

    Hi all,

    I want a ListView to show only a particular number of it's items no matter what the height of the ListView is. To get this I set the height of each item to the height of the ListView divided by the number of visible items. That works so far.
    I have an issue with the time it takes to create this ListView (startup time). While populating the ListView with it's delegates ListView.view.height seems to be 0 what causes the ListView to create all 10000 items, cause they all fit into the view. Afterwards the ListView.view.height gets it's actual value and 9996 items get deleted again.
    Here's my code:

    ListView {
      anchors.fill: parent
      cacheBuffer: 0
      model: 10000
      property int visibleItemsCount: 4
      delegate: Text {
        width: ListView.view.width
        height: ListView.view.height / ListView.view.visibleItemsCount
        text: index
        Component.onCompleted: console.log("c", index)
        Component.onDestruction: console.log("d", index)
      }
    }
    

    What can I do to get the ListView to only create visibleItemsCount number of items?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      There is no direct way to this as the model is definited with count. Either u use proxy model and try controlling items or try exposing model through c++ side and update model with required number of elements.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • GrecKoG Online
        GrecKoG Online
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        You could check if the height of the listView is null and return a sensible height if that's the case.
        height: ListView.view.height ? (ListView.view.height / ListView.view.visibleItemsCount) : implicitHeight

        DuBuD 1 Reply Last reply
        1
        • GrecKoG GrecKo

          You could check if the height of the listView is null and return a sensible height if that's the case.
          height: ListView.view.height ? (ListView.view.height / ListView.view.visibleItemsCount) : implicitHeight

          DuBuD Offline
          DuBuD Offline
          DuBu
          wrote on last edited by
          #4

          @GrecKo Thank you very much! Your solution led my girlfriend to an idea which inspired me to the following perfectly working solution:

          height: (ListView.view.height || 1) / ListView.view.visibleItemsCount
          

          I would call it a bug of ListView to put all (!) elements of the model into the view if the view's and the delegates's height both are 0.
          Thanks again!

          1 Reply Last reply
          1

          • Login

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