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. Set minimum height of QTextEdit to 1 text line. Or find all margins/padding...
Forum Updated to NodeBB v4.3 + New Features

Set minimum height of QTextEdit to 1 text line. Or find all margins/padding...

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.6k 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.
  • T Offline
    T Offline
    toralf
    wrote on last edited by toralf
    #1

    Hi.

    I'm trying to set the minimum height for a QTextEdit to a value equivalent to one line of text in the default font. Or differently put, I want to make sure at least one line is always visible without scrolling, but I won't worry about the fact that it's technically possible to set up a text size different from the one given by the "built-in" font. I also assume that there won't be a horizontal slider, since the widget is set up for text wrapping.

    What I've tried is:

    QFontMetrics metrics=ui->text->fontMetrics();
    ui->text->setMinimumHeight(metrics.height());
    

    But obviously, this won't quite work - the problem is that it doesn't take into account the fact that there will be margins and padding around the text. So I need

    1. Some way to find the total amount of extra space around the text.
      or
    2. A way to set the minimum text contents size and have the widget size adapt to this (if you know what I mean.)

    Regarding 1, I guess QWidget::getContentsMargin() and QTextDocument::documentMargin() might return parts of the space, but they don't seem to account for all of it - maybe there is additional padding from the style, or something.

    Help, anyone?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      toralf
      wrote on last edited by
      #2

      OMG. I think I got my maths wrong earlier. When checking again, it appears that the above mentioned properties do account for the entire space around the text - I get a correct height for one line if I add the top and bottom contents margins and twice the document margin to the font height.

      It would be nice to know, however, if this will generally be the case, or if there could be additional space in other setups. Actually, I'm assuming I also ought to consider the contents margin of the viewport, which is 0 by default on my system, but might there be anything else? What if a padding value is specified in a a style-sheet, for instance?

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Well it supports the box model so
        QTextEdit {
        padding-top: 25;
        }
        will alter the space calculation :)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toralf
          wrote on last edited by
          #4

          Exactly. I ought to have tested that earlier, I guess, but it certainly adds extra space, and it doesn't seem to count towards the "contents margin" or affect the "contents rectangle", so how does one find it?

          Maybe I won't worry about custom style settings in the case I'm working on now, but it seems to me that there ought to be a way to query all parameters related to sizes and spacing.

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            well
            contentsMargins()
            seems to reflect the padding-X

            1 Reply Last reply
            0
            • T Offline
              T Offline
              toralf
              wrote on last edited by
              #6

              I believe you are right. The padding is reflected both in contentsMargins() and contentsRect().

              Once again, I seem to get a different result when I test again something I thought I tested the same way earlier. What the ... Well, maybe I can blame it on the fact that I'm just back from holiday or something :-/

              mrjjM 1 Reply Last reply
              0
              • T toralf

                I believe you are right. The padding is reflected both in contentsMargins() and contentsRect().

                Once again, I seem to get a different result when I test again something I thought I tested the same way earlier. What the ... Well, maybe I can blame it on the fact that I'm just back from holiday or something :-/

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @toralf
                Well I guess its just how it is sometimes and maybe its just a divine power that has a thing for disproving that
                computers are deterministic :)

                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