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. [SOLVED] QML list view change item size to fit text

[SOLVED] QML list view change item size to fit text

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 16.7k 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
    Dolphin
    wrote on last edited by
    #1

    My listview has various item text lengths - it is really a menu so needs the flexibility of re-sizing itself for possible string lengths. This is for visually impaired so flickable does not seen like a sensible option (everything needs to stay still). Wrapping works but that means the 2nd line of text is outside the item. Using paintedHeight/width does not work.

    Any ideas?

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

      You could calculate item height from Text element (that is inside item) and thus you are safe to use wrapping.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dolphin
        wrote on last edited by
        #3

        Thanks for your reply. Could you possibly tell what that would look like in this code please? I cannot get it to work.

        @
        import QtQuick 2.0

        Rectangle
        {
        //align menu underneath prompts in centre
        height: parent.height - prompts.height
        width: parent.width

        Component
        {
            id: menuEntryDelegate
        
            Rectangle
            {
                id: wrapper
                width: menuHolder.width
                height: 50
                state: ListView.isCurrentItem ? "selected" : "notselected"
        
                Text  //TBD keep text inside box
                {
                    id: menuEntry
                    font.pointSize: 20
                    width: parent.width
                    wrapMode: Text.WordWrap
                    text: getDisplayString()
                    clip: true
                }
        

        ...
        ...
        }
        }

        Rectangle
        {
            id: menuContainer
            width: 400
            height: (50 * 9)
            anchors.horizontalCenter: parent.horizontalCenter
        
            ListView
            {
                id: menuHolder
                model: menuModel
                anchors.fill: parent
                opacity: 1
        
                header: Rectangle
                {
                    width: menuHolder.width
                    height: 50
                    color: "#2A51A3"
        
                    Text //TBD txt to say inside box
                    {
                       id: header
                       anchors.centerIn: parent
        
                       text: coreMenu.getMenuTitle()
                       font.pointSize: 20
                       color: "green"
                       width: parent.width
                       wrapMode: Text.WordWrap
                    }
                }
        
                delegate: menuEntryDelegate
                focus: true
        
                add: Transition
                {
                    NumberAnimation { properties: "x,y" }
                }
        
                Keys.onPressed:
                {
        

        ...
        ...
        }
        }
        }
        }
        @

        1 Reply Last reply
        0
        • D Offline
          D Offline
          daliusd
          wrote on last edited by
          #4

          In wrapper Rectangle set height based on menuEntry height. You might want to center Text inside wrapper, as well set smaller width, specify spacing and etc.

          @
          Rectangle
          {
          id: wrapper
          width: menuHolder.width
          height: menuEntry.height * 1.5 + 20 // * 1.5 if you are using spacing I believe default is 1.0
          state: ListView.isCurrentItem ? "selected" : "notselected"

                  Text  //TBD keep text inside box
                  {
                      id: menuEntry
                      font.pointSize: 20
                      width: parent.width
                      wrapMode: Text.WordWrap
                      text: getDisplayString()
                      clip: true
                  }
          

          @

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dolphin
            wrote on last edited by
            #5

            Thank you :-) :-)

            1 Reply Last reply
            0
            • D Offline
              D Offline
              daliusd
              wrote on last edited by
              #6

              I will just share another little trick I learned recently: you might want to have different height and contentHeight on Text element. contentHeight is height of text, height is how much height occupies Text element (usually it is the same but there might be situations where you would like to have height bigger than contentHeight).

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dolphin
                wrote on last edited by
                #7

                Great thanks :-)

                Any way to keep the content within the view when you scale? This is for people with visual imparement so it panning must be kept to a minimum. Wrapping occurs when you increase content or font size but not if you scale. Is the only way round that to not use the scaling but calculate everything my self (probably just increasing the font manually)????

                (By scaling I mean using myThing.scale)

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  daliusd
                  wrote on last edited by
                  #8

                  I personally have not used .scale at all.

                  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