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. QML: calculate height with min/max range for Rectangle from child Text content

QML: calculate height with min/max range for Rectangle from child Text content

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 6.9k 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.
  • E Offline
    E Offline
    eva_hxx
    wrote on 14 Apr 2014, 03:06 last edited by
    #1

    Hello,
    I’m new to QML and have following problem, some like [[SOLVED] QML: calculate size for Rectangle from child Text content]:
    I have a Rectangle element with the Text element inside of it. The Rectangle has Stationary Width 200, Minimum Height 100 and Maximum Height 200.
    Text of Text element is dynamic and wrapped by the rectangle height, font size is also dynamic.

    I need to resize my rectangle by height according to some spec:

    1. if text paintedHeight is smaller than Minimum Height 70(margin is 15), we will show the text in the center of the rectangle 200x100
    2. if text paintedHeight is between 70 and 270, we will show the text in the center of the rectangle 200x(paintedHeight +30)
    3. if text paintedHeight is bigger than Maximum Height 170, we will show the text in the center of the rectangle 200x200, and I want to elide the Text, but the problem is that elideRight will cause the text show in one line; and if I use clip, text in last line will be clipped and this is not friendly.

    @
    __import QtQuick 1.1

    Rectangle {
    width: 200
    height: contentText.height + 30//the top margin + bottom margin
    Text {
    id: contentText
    anchors.centerIn: parent

        text: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyxxxxxxxxxxxxxxxxxxxxxxxxxdddddddddd"
        font.pixelSize: 30 //or regular or big
        width: parent.width - 30//the left margin + right margin
        height: Math.max(Math.min(paintedHeight, 70), 170)//range is [70, 170]
        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
        clip: true
        //elide: Text.ElideRight
    }
    

    }__
    @

    Thanks a lot!

    [edit: please add @ tags at the beginning and en of code fragments, Eddy]

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eddy
      wrote on 14 Apr 2014, 07:53 last edited by
      #2

      Hi and welcome to devnet,

      Is there a special reason why you use an old version of Qt Quick?

      In version 2 there are some interesting features added which might help you. Have a look at "this overview":http://qt-project.org/doc/qt-5/qtquickcontrols-overview.html

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eva_hxx
        wrote on 15 Apr 2014, 09:16 last edited by
        #3

        Thanks for your update!
        Maybe we will upgrade to version 2 finally, but not in the next two months, so this is still a bug for me.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chrisadams
          wrote on 16 Apr 2014, 02:15 last edited by
          #4

          You can use implicitHeight and implicitWidth to help with this sort of thing, if you are not using higher-level controls to help you with layouts.

          1 Reply Last reply
          0

          3/4

          15 Apr 2014, 09:16

          • Login

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