Important: Please read the Qt Code of Conduct -

How to design a solution for QLabel text alignment?

  • Hi Everyone,

    QT newbie here.

    Just wondering if there is a way of designing a solution using QT for this requirement.

    I have two labels.

    Font: Arial (non monspace font)

    label1.text: $0.00
    label2text: $45.00

    $ amounts are variable in nature.
    The label should be displayed in such a way that it appears like the . are aligned as per the below pics



    I tried adding the QLabels (label1 and label2) to QVBoxLayout. But it didn't help.

    Any solution to this kind of problem?

    Any advice will be appreciated.


  • Lifetime Qt Champion

    What's wrong with QLabel::alignment()?

  • Moderators

    @Christian-Ehrlicher I'm guessing the problem is this part:

    Font: Arial (non monspace font)

    With monospace aligning right would be enough but the . will float around with non-monospace.

    @Blackknight As a sort of workaround you could use two labels next to each other - first one displaying the part up to . right aligned and the other the rest aligned to left. This is a no go if you want the labels to be interactive i.e. wouldn't work for selecting with mouse for example.

  • @Chris-Kawa
    I thought about saying this, but I think the OP is only asking for simple right-justify (edit hmm, maybe I'm wrong). What I did not notice is this font looks like similar-width digits, the OP might just want to forbid any digits after the decimal point which are 1 ;-)

    If you are talking about the pixel-level of alignment on the .s, then you really must/ought use monospace. To me in any case, if you want columns of figures to line up nicely (e.g. for running your eye down), it's not just the place of the decimal point that matters, it's all the columns of digits. Else once there are some 1s it's all out of alignment.

  • @Chris-Kawa

    Thanks for your advice.

    Here are my options:

    Option 1: Use a monospace font and discard Arial. I can then use QString rightJustified(..)

    Option 2: Try what @Chris-Kawa has suggested

    Option 3: Use Arial font, but use fill character "figure space" or "thin space" instead of space " " which is non-monospaced. I don't know if this is possible in QT. Please let me know if this is workable.


  • Is there any way of doing this?

    I saw that input to be right-justified has a range:

    //$X.XX e.g. $5.00
    //$XX.XX e.g. $50.00
    //$XX.XX e.g. $500.00
    //$XX.XX e.g. $4999.00 (Max)

    If my QString value1 contains the above strings, I count the characters e.g.
    8 (Max)

    Then I take the value 2 let us say $2.36 which will always be 5 chars and do:

    value2 = value2.rightJustified(lengthOfValue1String,QLatin1Char(' '), true);

    The problem obviously is non-monospaced Arial font.

    But I noticed that if I do QLatin1Char('$') instead of space, both values are aligned as per the images in my original post. But with $ instead of space which I don't want.

    Is there a way of using the fillChar with $ and then make them invisible? : )

Log in to reply