Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to design a solution for QLabel text alignment?
Qt 6.11 is out! See what's new in the release blog

How to design a solution for QLabel text alignment?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blackknight
    wrote on last edited by
    #1

    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.
    label1
    label2

    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

    Sample1
    sample1.png

    Sample2
    sample.png

    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.

    Thanks.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        JonBJ 1 Reply Last reply
        2
        • Chris KawaC Chris Kawa

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 ;-)

          @Blackknight
          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.

          1 Reply Last reply
          1
          • B Offline
            B Offline
            Blackknight
            wrote on last edited by Blackknight
            #5

            @Chris-Kawa
            @JonB

            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.

            Thanks.

            B 1 Reply Last reply
            0
            • B Blackknight

              @Chris-Kawa
              @JonB

              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.

              Thanks.

              B Offline
              B Offline
              Blackknight
              wrote on last edited by Blackknight
              #6

              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.
              5
              6
              7
              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? : )

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved