Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Find what you need - explore our website and developer resources
18 December 2024
Sometimes the simple questions come with a super complex answer.
Question: How do you draw a rectangle around a piece of text?
Answer: Uh ohh, hold my beer (or coffee or ...) .....
QFontMetrics::width() doesn't exists anymore, instead it suggest to call QFontMetrics:::horizontalAdvance. That, however, returns a too tight box.
Then there is QFontMetrics::boundingRect, but at first sight that just return something really weird.
Then there is QPainter::boundingRect, but just like horizontalAdvance, that is too tight too...
But in the end Jesper, untangles it all and succeeds!
Chapters
0:00 Intro
0:30 What triggered this episode
01:22 QFontMetrics::width is depreciated
02:24 QFontMetrics::horizontalAdvance draws outside the box
3:22 documentation for horitontalAdvance
6:36 QFontMetrics::boundingRect
9:58 OK, lets try and translate the boundingRect to (0,0)
10:47 Let's try and translate by (leftBerring, accent)
12:50 QPainter::boundingRect
14:25 Lets just try and add some space...
17:00 QFontMetrics::tightBoundingRect
17:36 Finally, I think I understood it!
22:45 Let me know your thoughts on this.
The example code showcased in this video is available here: https://github.com/KDABLabs/kdabtv/tree/master/Qt-Widgets-and-more/font-metrics
Link to Episode 41 - Module 6 - Introduction to Qt Widgets series: https://youtu.be/dhEdW4QBx70?list=PL6CJYn40gN6jIsRYriqN70V83t_mdDZHT