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 changes size when placed on Page with header

ListView changes size when placed on Page with header

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 753 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.
  • J Offline
    J Offline
    Justin Steventon
    wrote on last edited by
    #1

    Hi all,

    I have a small (self-contained) snippet of QML which has a bug that I am having trouble diagnosing.

    To reproduce the issue:

    • Scroll to the bottom of the page
    • Click the last list item
    • Click the back button

    The result is that the list view scroll position on the first page is incorrect. It is offset by the height of the header.

    Some other points:

    • If I listen to the ListView onHeightChanged event, then this is called when changing pages. This is what seems to be the cause.
    • The problem does not occur if I remove the Page header.

    Any idea what I am doing wrong?

    Thanks!

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import QtQuick.Layouts 1.12
    import QtQuick.Window 2.12
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Component {
            id: page1
            Page {
                header: ToolBar {}
                ListView {
                    anchors.fill: parent
                    model: 30
                    delegate: ItemDelegate {
                        width: parent.width
                        text: model.index
                        onClicked: stackView.push(page2)
                        font.pixelSize: 20
                        contentItem: RowLayout {
                            ColumnLayout {
                                Layout.fillWidth: true
                                Label {
                                    text: "Hello"
                                }
                                Label {
                                    text: "There_" + model.index
                                }
                            }
                        }
                    }
                }
            }
        }
    
        Component {
            id: page2
            Page {
                header: ToolBar {}
                Button {
                    anchors.centerIn: parent
                    width: 100
                    height: 100
                    text: "Back"
                    onClicked: stackView.pop()
                }
            }
        }
    
        StackView {
            id: stackView
            anchors.fill: parent
            initialItem: page1
        }
    }
    
    1 Reply Last reply
    0
    • YunusY Offline
      YunusY Offline
      Yunus
      wrote on last edited by Yunus
      #2

      Because your ListView is connected to parent's width and height. If you arrange width and height by manual, it ll be fixed. You can change listview with this:

      ListView {
                      //anchors.fill: parent
                      width: 480
                      height: 420
                      model: 30
                      delegate: ItemDelegate {
                          width: parent.width
                          text: model.index
                          onClicked: stackView.push(page2)
                          font.pixelSize: 20
                          contentItem: RowLayout {
                              ColumnLayout {
                                  Layout.fillWidth: true
                                  Label {
                                      text: "Hello"
                                  }
                                  Label {
                                      text: "There_" + model.index
                                  }
                              }
                          }
                      }
                  }
      
      J 1 Reply Last reply
      0
      • GrecKoG Online
        GrecKoG Online
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        @Justin-Steventon said in ListView changes size when placed on Page with header:

        The result is that the list view scroll position on the first page is incorrect. It is offset by the height of the header.

        I do not reproduce the problem on linux with Qt 5.15

        J 2 Replies Last reply
        1
        • GrecKoG GrecKo

          @Justin-Steventon said in ListView changes size when placed on Page with header:

          The result is that the list view scroll position on the first page is incorrect. It is offset by the height of the header.

          I do not reproduce the problem on linux with Qt 5.15

          J Offline
          J Offline
          Justin Steventon
          wrote on last edited by
          #4

          @GrecKo thanks, I am on 5.12.6. I will try out 5.15.

          1 Reply Last reply
          0
          • GrecKoG GrecKo

            @Justin-Steventon said in ListView changes size when placed on Page with header:

            The result is that the list view scroll position on the first page is incorrect. It is offset by the height of the header.

            I do not reproduce the problem on linux with Qt 5.15

            J Offline
            J Offline
            Justin Steventon
            wrote on last edited by
            #5

            @GrecKo this problem still occurs on 5.15 with Windows MSVC 19 x64.

            1 Reply Last reply
            0
            • YunusY Yunus

              Because your ListView is connected to parent's width and height. If you arrange width and height by manual, it ll be fixed. You can change listview with this:

              ListView {
                              //anchors.fill: parent
                              width: 480
                              height: 420
                              model: 30
                              delegate: ItemDelegate {
                                  width: parent.width
                                  text: model.index
                                  onClicked: stackView.push(page2)
                                  font.pixelSize: 20
                                  contentItem: RowLayout {
                                      ColumnLayout {
                                          Layout.fillWidth: true
                                          Label {
                                              text: "Hello"
                                          }
                                          Label {
                                              text: "There_" + model.index
                                          }
                                      }
                                  }
                              }
                          }
              
              J Offline
              J Offline
              Justin Steventon
              wrote on last edited by
              #6

              @Yunus this works. My ListView is a reusable component, so it would be nice if this worked more generally. Do you know if there is there a way of mimicing the effect of "anchors.fill: parent" without creating the side-effect? Thanks!

              J 1 Reply Last reply
              0
              • J Justin Steventon

                @Yunus this works. My ListView is a reusable component, so it would be nice if this worked more generally. Do you know if there is there a way of mimicing the effect of "anchors.fill: parent" without creating the side-effect? Thanks!

                J Offline
                J Offline
                Justin Steventon
                wrote on last edited by
                #7

                @Justin-Steventon I am going to mark this as resolved. The final solution, which at least handles the scenarios I am interested in:

                ListView {
                    width: parent.width
                    Component.onCompleted: height = parent.height
                    Connections {
                        target: appWindow    // main app window
                        onHeightChanged: listView.height = parent.height
                    }
                
                ...
                }
                
                1 Reply Last reply
                0

                • Login

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