Important: Please read the 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 ?


  • Lifetime Qt Champion


    Do you mean something like "QFontMetrics": ?

  • 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