Dynamic text sizing and alignment based on screen resolution



  • Hi everyone, I'm having an issue with how to display text that I hope you may be able to help with.

    I'm trying to display a title menu for a game, with six entries. Each would be a text string backed by a semi transparant backing and offset to the side of the screen to allow the artwork to show through. I am using the graphics view framework but manually drawing everything within the scene object myself because I want more depth layers than the framework offers.

    I want to minimise how much space these entries take up while still making them human readable, so ideally I would like to do the following:

    1. Calculate 10% of the screen height and select an appropriate font matching this size, vertical alignment is handled using this as a step value.
    2. Calculate how wide the text entries will be utilising this font and then use the appropriate horizontal alignment based on this value.
    3. Render the text to the screen.

    The second half of part (1) and part (2) have me really stumped at the moment. This is initially targetting Symbian but will be ported to desktop afterwards.

    Thank you kindly for any help.



    1. maybe something like this:
      @ QFont font("Arial");
      font.setPointSize(14);
      QFontMetrics fm(font);
      qDebug()<<fm.width("1234")<<"pix";@

    out:
    44 pix



  • That's fantastic, thank you! I had completely missed the QFontMetrics class.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.