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. Center list view vertically
Qt 6.11 is out! See what's new in the release blog

Center list view vertically

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 5.6k 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.
  • D Offline
    D Offline
    Donner
    wrote on last edited by
    #1

    Hey,

    I have a rectangle with a specific height (which doesn't change) and a ListView inside this rectangle. Normally I use anchors.fill: parent on the listview, but now I get this problem: When the listview contains only 2-3 elements, it is smaller than the rectangle so there is space over below the last item.
    What can I do if I want to center the items like in the picture below:
    !http://i.imgur.com/J9u6e.png(QML ListView: Layout centered)!

    I tried to use something like this:
    @ ListView {
    anchors.fill: parent // When not having this line, the list has height 0
    anchors.verticalCenter: parent.verticalCenter

        model: testmodel
        delegate: Text {
            text: name + ": " + number
        }
    }@
    

    It didn't work.

    Does anyone know how to achieve something like that?
    Thanks in advance!
    D.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QtRoS
      wrote on last edited by
      #2

      You may put your ListView in another container such as Item, which will be vertically centered in main Container from picture. And don't forget to set inner Item height to -listView.height- and width to parent.width.
      UPD use listView.contentHeight instead of listView.height. Of maybe you will need their combination.
      I can't test it now, but it should works.

      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