Get the text margins of a QLineEdit
Asked Answered
A

1

6

How can I get the text margins for a QLineEdit?

Note the red lines in the screenshot, that's the margins I'm interested in:

enter image description here

I tried qDebug() << ui->lineEdit->textMargins(); for the edit box from the screenshot, but it printed QMargins(0, 0, 0, 0), even though the edit box does have margins.

Av answered 16/4, 2014 at 7:19 Comment(4)
Eventually this is the space between font-size and widget-size? You probably could calculate this by size of font (QFontMetrics) and the available height. The textMargins() method returns the reserved free space for margins, a margin of 0 does not imply your font will always hit the borders.Vitrify
That could account for the vertical margins, but not for the horizontal one. Also, the horizontal one can disappear, see i.sstatic.net/9mD13.pngAv
Why do you need this information? maybe there is alternative way to solve your problem without getting this values.Kitts
@MarekR I need to calculate the absolute bounding box of a given character. Why do I need that? Long story.Av
K
3

Check paint event of QLineEdit. As you can see lots of stuff have effect on it.

Besides textMargins there are stuff which can have impact on values which you are looking for.

  1. used style (see line 1942)
  2. and font properties - see QFontMetrics::ascent and QFontMetrics::descent

The style value is most important and you have to find way to get this value (probably subclass is needed).

Kitts answered 16/4, 2014 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.