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 vertically item on Flickable

Center vertically item on Flickable

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

    Hi,

    I can't center vertically Label on Flickable as you can see below :

    0_1500990774539_pagetitle.png

    Page {
    
        // Title
        Loader {
            id: title
            anchors {
                top: parent.top
                topMargin: 10
                left: parent.left
                leftMargin: 10
                right: parent.right
                rightMargin: 10
            }
            source: "TitleLabel.qml"
        }
    
        Flickable {
            interactive: true
            anchors {
                top: title.bottom
                topMargin: 10
                bottom: buttonValidate.top
                bottomMargin: 10
                left: parent.left
                leftMargin: 10
                right: parent.right
                rightMargin: 10
            }
            clip: true
            boundsBehavior: Flickable.StopAtBounds
            flickableDirection: Flickable.AutoFlickDirection
            contentHeight: labelMessage.implicitHeight
    
            Label {
                id: labelMessage
                anchors.centerIn: parent
                width: parent.width
                horizontalAlignment: Text.AlignHCenter
                wrapMode: Text.Wrap
                text: displayMessage.message
                font.pixelSize: 20
            }
    
            ScrollBar.vertical: ScrollBar { }
        }
    
        Button {
            id: buttonValidate
            anchors {
                bottom: parent.bottom
                bottomMargin: 10
                left: parent.left
                leftMargin: 10
                right: parent.right
                rightMargin: 10
            }
    
            contentItem: Text {
    ...
            }
    }
    

    "anchors.centerIn: parent" isn't working since I surrounded Label with Flickable.

    Any idea?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      deleted357
      wrote on last edited by
      #2

      Please help!

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbnoimi
        wrote on last edited by
        #3

        Try to use:

        anchors.left: parent.left
        anchors.right: parent.right
        
        1 Reply Last reply
        0
        • D Offline
          D Offline
          deleted357
          wrote on last edited by deleted357
          #4

          Not working! Label isn't vertically centered.

              Label {
                  id: labelMessage
                  anchors.top: parent.top
                  anchors.bottom: parent.bottom
                  anchors.left: parent.left
                  anchors.right: parent.right
                  anchors.centerIn: parent
                  width: parent.width
                  horizontalAlignment: Text.AlignHCenter
                  wrapMode: Text.Wrap
                  text: displayMessage.message
                  font.pixelSize: 20
              }
          
          1 Reply Last reply
          0
          • timdayT Offline
            timdayT Offline
            timday
            wrote on last edited by timday
            #5

            Personally, when I have problems like this I replace anchor-based layout with explicit x,y positioning (e.g using y: 0.5*(parent.height-height) to vertically centre something) and add some console.log output of x, y, width & height elements looking for where my assumptions are wrong. And they always are wrong somewhere.

            1 Reply Last reply
            0
            • YashpalY Offline
              YashpalY Offline
              Yashpal
              wrote on last edited by
              #6

              @safi_bachelet said in Center vertically item on Flickable:

              contentHeight: labelMessage.implicitHeight

              You have set the flickable contentHeight to label height, that is limiting your flickable area to label height.

              1. Remove contentHeight: labelMessage.implicitHeight to vertically center label on Flickable.
              2. Also, you can play around with 'contentHeight' by giving different value and understand the contentHeight property better.
              1 Reply Last reply
              1
              • D Offline
                D Offline
                deleted357
                wrote on last edited by
                #7

                I found the solution thanks to you:

                Flickable {
                    id: flickableMessage
                    ...
                    contentHeight: labelMessage.implicitHeight > flickableMessage.height ? labelMessage.implicitHeight : flickableMessage.height
                
                    Label {
                        id: labelMessage
                        ...
                    }
                
                    ScrollBar.vertical: ScrollBar { }
                }
                

                Thanks guys.

                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