1

I have a QT application with a simple QLabel. When I tries to align text, it seems to works correctly,

m_c_label.setAlignment(Qt::AlignLeft |Qt::AlignVCenter);

but if you look with attention, text is not vertically centered correclty, is displaced a few pixels to the bottom. I have tried with different layout configurations and always is the same problem.

It's a Qt 4.6 embedded application with qpf2 fonts. I have tried to change this font and with another font the text is desplaced to font so it seems to be a strange behaviour with the font. Any idea?

Xabi E
  • 251
  • 2
  • 3
  • 15
  • 1
    Please attach a screenshot to show what the problem is. Maybe your font is weirdly aligned to the bottomline of the text ? – ymoreau Oct 29 '18 at 11:08
  • It's a embedded system and i can't take a snapshot in an easy way. May be this could be the problem, but how I can see that in the font? – Xabi E Oct 29 '18 at 14:59
  • My guess is that the vertical alignment is based on the ascent and descent properties of the associated [`QFontMetrics`](http://doc.qt.io/qt-5/qfontmetrics.html) rather than the \`tight' bounding rectangle of the actual text displayed. Otherwise changing the text repeatedly would cause the apparent baseline of the text to jitter vertically. – G.M. Oct 29 '18 at 15:18
  • I've been testing different solutions, like [link](https://stackoverflow.com/questions/32404596/qpainter-with-qtaligncenter-does-not-center-text-correctly) but I always get the same result. My problem is text thasn't fit in the rect area if it is centered so I want to aling text almost centered, but 1 pixel up. Any idea? – Xabi E Oct 31 '18 at 16:20
  • 1
    Maybe the text is correctly centered in the label but the label isn't correctly centered in its layout? – rubenvb Jan 18 '22 at 10:07
  • 2
    `m_c_label.setAlignment(Qt::AlignCenter)` these works for me, but in case it doesn't for you, please i suggest you consider posting a minimal reproducible code because there may be some other widget or layout interferring –  Jan 18 '22 at 10:14
  • 1
    @XabiE please can you post a screenshot or a minimal reproducible code –  Jan 18 '22 at 10:26

0 Answers0