Results 1 to 4 of 4

Thread: How to format a string to a const width padding with space at the tail

  1. #1

    Default How to format a string to a const width padding with space at the tail

    I want to format a string to a const width. if the length of string is less than the const value, then padding it with space char at the tail.
    For example, with const = 5,
    "a" is formatted to "a "
    "ab" is formatted to "ab "
    "abc" is formatted to "abc "
    "abcd" is formatted to "abcd "
    "abcde" is formatted to "abcde"

    thanks!

  2. #2
    Join Date
    Jul 2008
    Posts
    47
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to format a string to a const width padding with space at the tail

    This is the same answer as with leading "0" for numbers. But instead of right you do a left.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to format a string to a const width padding with space at the tail

    Do u want spaces to span the 5-x length ? or a single space if the length is less than 5 ??
    If its latter, you can do it as -
    Qt Code:
    1. if(myString.lengt()<5)
    2. myString.append(" ");
    To copy to clipboard, switch view to plain text mode 

    if u want the former way then -
    Qt Code:
    1. myString.append( QString("").fill(MAX-myString.length());
    To copy to clipboard, switch view to plain text mode 
    Hope u get the idea,
    For numbers, u can use prepend with the same logic

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to format a string to a const width padding with space at the tail

    Take a look at QString::arg() parameters fieldWidth and fillChar:
    A positive value will produce right-aligned text, whereas a negative value will produce left-aligned text.
    J-P Nurmi

Similar Threads

  1. how to format a number into a const width string ?
    By lovelypp in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 21:45
  2. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  3. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.