Problems with Open-Source Downloads read and

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

  • 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();

    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.
    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?

  • 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?

  • Lifetime Qt Champion

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

  • 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.

  • Lifetime Qt Champion

    seems to reflect the padding-X

  • 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 :-/

  • Lifetime Qt Champion

    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 :)

Log in to reply