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. QString Formatting - Lining up multiple QStrings

QString Formatting - Lining up multiple QStrings

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.8k Views
  • 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.
  • R Offline
    R Offline
    RyanAR
    wrote on 28 Feb 2022, 14:50 last edited by
    #1

    Hello,

    I am currently trying to line up text within a QListWidget so that the temperature, percentage and status are always on the same vertical line.

    Example of what I'm aiming for
    16079a90-db4a-42df-8935-98d490399386-image.png

    What I get
    46017498-818f-4e2c-8c77-433ba6ef2817-image.png

    I am using this line of code to format each row, which as you can see doesn't quite line up properly.

    QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -16, ' ').arg(cpuTemp + "C", -4, ' ').arg(cpuUsage + "%", -4, ' ').arg(isOnline);
    

    Does anyone have any suggestions on how to perfectly align these rows?

    Hostname: Max 18 characters
    Temperature: Max 3 characters
    Usage: Max 3 characters
    Status: Max 1 character

    Any help would be greatly appreciated!

    Thanks,
    Ryan

    K J 2 Replies Last reply 28 Feb 2022, 14:55
    0
    • R RyanAR
      28 Feb 2022, 14:50

      Hello,

      I am currently trying to line up text within a QListWidget so that the temperature, percentage and status are always on the same vertical line.

      Example of what I'm aiming for
      16079a90-db4a-42df-8935-98d490399386-image.png

      What I get
      46017498-818f-4e2c-8c77-433ba6ef2817-image.png

      I am using this line of code to format each row, which as you can see doesn't quite line up properly.

      QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -16, ' ').arg(cpuTemp + "C", -4, ' ').arg(cpuUsage + "%", -4, ' ').arg(isOnline);
      

      Does anyone have any suggestions on how to perfectly align these rows?

      Hostname: Max 18 characters
      Temperature: Max 3 characters
      Usage: Max 3 characters
      Status: Max 1 character

      Any help would be greatly appreciated!

      Thanks,
      Ryan

      K Offline
      K Offline
      KroMignon
      wrote on 28 Feb 2022, 14:55 last edited by KroMignon
      #2

      @RyanAR said in QString Formatting - Lining up multiple QStrings:

      Any help would be greatly appreciated!

      If you want to use spaces to align text, you also have to use a monospaced font and not a proportional font.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      3
      • R RyanAR
        28 Feb 2022, 14:50

        Hello,

        I am currently trying to line up text within a QListWidget so that the temperature, percentage and status are always on the same vertical line.

        Example of what I'm aiming for
        16079a90-db4a-42df-8935-98d490399386-image.png

        What I get
        46017498-818f-4e2c-8c77-433ba6ef2817-image.png

        I am using this line of code to format each row, which as you can see doesn't quite line up properly.

        QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -16, ' ').arg(cpuTemp + "C", -4, ' ').arg(cpuUsage + "%", -4, ' ').arg(isOnline);
        

        Does anyone have any suggestions on how to perfectly align these rows?

        Hostname: Max 18 characters
        Temperature: Max 3 characters
        Usage: Max 3 characters
        Status: Max 1 character

        Any help would be greatly appreciated!

        Thanks,
        Ryan

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 28 Feb 2022, 14:57 last edited by
        #3

        @RyanAR if you want to use String formatting to align those columns, than you have to use a Monospaced font, so that each character has the same width.

        alternatively, what I would suggest, using a QListView and defining your own Delegate, that shows the data.
        That way you can use QFontMetrics to get the max width of each text in one column, and position the texts accordingly


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        2
        • R Offline
          R Offline
          RyanAR
          wrote on 28 Feb 2022, 15:20 last edited by
          #4

          Can't believe it was that simple!

          Added "font-family:courier;" to the CSS of the QListWidget.

          dcb849ab-b287-4240-869f-ef0884bd95cd-image.png

          Thanks for the help @KroMignon @J-Hilk!

          J K 2 Replies Last reply 28 Feb 2022, 15:24
          1
          • R RyanAR
            28 Feb 2022, 15:20

            Can't believe it was that simple!

            Added "font-family:courier;" to the CSS of the QListWidget.

            dcb849ab-b287-4240-869f-ef0884bd95cd-image.png

            Thanks for the help @KroMignon @J-Hilk!

            J Offline
            J Offline
            JonB
            wrote on 28 Feb 2022, 15:24 last edited by
            #5

            @RyanAR
            If you felt really brave. you'd align your percentage numbers so they took 2 positions right-justified (single digits like you show would have an extra space before them), then it would like really neat :)

            1 Reply Last reply
            1
            • R RyanAR
              28 Feb 2022, 15:20

              Can't believe it was that simple!

              Added "font-family:courier;" to the CSS of the QListWidget.

              dcb849ab-b287-4240-869f-ef0884bd95cd-image.png

              Thanks for the help @KroMignon @J-Hilk!

              K Offline
              K Offline
              KroMignon
              wrote on 28 Feb 2022, 15:25 last edited by
              #6

              @RyanAR said in QString Formatting - Lining up multiple QStrings:

              Can't believe it was that simple!

              You'r welcome... Sometimes solution is so evident that we becomes blind and can not see it ;)

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1
              • R Offline
                R Offline
                RyanAR
                wrote on 28 Feb 2022, 15:56 last edited by
                #7

                @JonB - I did a brave! Thanks for the suggestion. Hostname is left justified, and the rest are now right justified, so all the symbols match up in all scenarios. I also added a max character limit on the hostname to replace trailing characters with ".." if the name is too large.

                if(hostname.length() > 15)
                 {
                   hostname.chop(hostname.length() - 15);
                   hostname = hostname + "..";
                 }
                

                0cd6b58a-be50-4cc8-9cb5-27ccc2c33500-image.png

                QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -18, ' ').arg(cpuTemp + "C", 4, ' ').arg(cpuUsage + "%", 4, ' ').arg(isOnline);
                
                J K 2 Replies Last reply 28 Feb 2022, 16:09
                2
                • R RyanAR
                  28 Feb 2022, 15:56

                  @JonB - I did a brave! Thanks for the suggestion. Hostname is left justified, and the rest are now right justified, so all the symbols match up in all scenarios. I also added a max character limit on the hostname to replace trailing characters with ".." if the name is too large.

                  if(hostname.length() > 15)
                   {
                     hostname.chop(hostname.length() - 15);
                     hostname = hostname + "..";
                   }
                  

                  0cd6b58a-be50-4cc8-9cb5-27ccc2c33500-image.png

                  QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -18, ' ').arg(cpuTemp + "C", 4, ' ').arg(cpuUsage + "%", 4, ' ').arg(isOnline);
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 28 Feb 2022, 16:09 last edited by
                  #8

                  @RyanAR
                  I know it's an OCD of mine to line up numbers by right-justifying them, but doesn't it look nicer now? :) So much easier for a human reader to grasp those figures.....

                  1 Reply Last reply
                  1
                  • R RyanAR
                    28 Feb 2022, 15:56

                    @JonB - I did a brave! Thanks for the suggestion. Hostname is left justified, and the rest are now right justified, so all the symbols match up in all scenarios. I also added a max character limit on the hostname to replace trailing characters with ".." if the name is too large.

                    if(hostname.length() > 15)
                     {
                       hostname.chop(hostname.length() - 15);
                       hostname = hostname + "..";
                     }
                    

                    0cd6b58a-be50-4cc8-9cb5-27ccc2c33500-image.png

                    QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -18, ' ').arg(cpuTemp + "C", 4, ' ').arg(cpuUsage + "%", 4, ' ').arg(isOnline);
                    
                    K Offline
                    K Offline
                    KroMignon
                    wrote on 28 Feb 2022, 16:15 last edited by
                    #9

                    @RyanAR said in QString Formatting - Lining up multiple QStrings:

                    if(hostname.length() > 15)
                    {
                    hostname.chop(hostname.length() - 15);
                    hostname = hostname + "..";
                    }

                    Looks strange to me... I would change this to:

                    if(hostname.length() > 18) // as 18 is the max expected length
                        hostname  = hostname.left(15)+"...";
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    1

                    1/9

                    28 Feb 2022, 14:50

                    • Login

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