Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Text label with irregular shape?
Forum Updated to NodeBB v4.3 + New Features

Text label with irregular shape?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 234 Views 2 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    Is there a simple way to format a textual label to follow an irregular border?

    Here is an example taken from the current UML standard (2.5.1) illustrating a comment note:
    comment_original.png
    If I draw the text within the green rectangle in the following image, there is always the possibility that it will collide with the turned-down corner at the upper right:
    comment_green_rectangle.png
    However, is it possible to define a closed path as in the pink shape in the following image and have the text constrained to the enclosed area?
    comment_pink_rectangle.png
    Of course, we have no way of knowing how the original image was created, but for such purposes there should be a way of automatically formatting any kind of text.

    If I constrain the text to the area of the rectangle whose width fits the distance between the left-hand border and the edge of the turned-down corner, it leaves too much space at the right of the rest of the text.

    What is the best way of approaching this?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      You can use QTextLayout. You define line boxes and it breaks the text apart to fit into these boxes. The docs have an example code how to use it.

      For your particular example it's simple enough - just have the first line box a bit shorter and then all the other ones longer.

      For a general case of any shape it's similar, but you first have to find the boxes that fit into the shape. There's nothing in Qt (that I know) to do it for you, but the algorithm is well documented, as it's used e.g. in the CSS spec for the shape-inside property. Basically you rasterize the shape into a grid and find the cell groups that don't intersect the shape, as described e.g. here (there's a nice visualization at the bottom).

      R 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        You can use QTextLayout. You define line boxes and it breaks the text apart to fit into these boxes. The docs have an example code how to use it.

        For your particular example it's simple enough - just have the first line box a bit shorter and then all the other ones longer.

        For a general case of any shape it's similar, but you first have to find the boxes that fit into the shape. There's nothing in Qt (that I know) to do it for you, but the algorithm is well documented, as it's used e.g. in the CSS spec for the shape-inside property. Basically you rasterize the shape into a grid and find the cell groups that don't intersect the shape, as described e.g. here (there's a nice visualization at the bottom).

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        Thanks very much @Chris-Kawa ... this is exactly what I was hoping for.

        1 Reply Last reply
        0
        • R Robert Hairgrove has marked this topic as solved on

        • Login

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