Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Ascertaining text width [solved]



  • Hi,

    I've looked through the documentation, and not found the answer (or maybe missed it) so I'll ask here. Apologies if the answer is in the docs.

    Imagine I have a button, built as an image so it can have complex graphics affects, say. If I want the button to expand horizontally, I can extract the central column and replicate. On this image I have some text. The text can be anything, in any font, font size, etc. The app wants to expand the image to fit the text, but is there a way to find the width of the text, given the text itself can be of any length and any font attributes ? Some method to get the width of the given text string when rendered in the font specified, so that width can then be used to control the button/image width ?

    Thanks


  • Lifetime Qt Champion

    Hi,

    Do you mean something like "QFontMetrics":http://qt-project.org/doc/qt-4.8/qfontmetrics.html ?



  • Perfect ! Couldn't ask for a better response. Many thanks, much appreciated.


  • Lifetime Qt Champion

    You're welcome !

    Don't forget to update the thread's title to solved so other users may know a solution has been found :)


Log in to reply