Results 1 to 13 of 13

Thread: QString and .arg()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: QString and .arg()

    What's your Qt's version? This code works fine for me.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  2. #2
    Join Date
    Jun 2012
    Posts
    173
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 2 Times in 2 Posts

    Default Re: QString and .arg()

    it is Qt 4.8.1 ... you mean you got the spaces after the name like
    "Student Name******** = jemes" ??

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: QString and .arg()

    I'm also using 4.8.1 and I've got such output:
    int main(int, char**) " Student Name = James"
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Jun 2012
    Posts
    173
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 2 Times in 2 Posts

    Default Re: QString and .arg()

    the editor eat the space but, i see the spaces you got same which is in the front : "********Student Name = James"

    but the result i want is "Student Name******** = James" .

    "*": means space

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: QString and .arg()

    see Oleg's post above.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanks
    10
    Thanked 27 Times in 22 Posts

    Default Re: QString and .arg()

    From the docs:
    fieldWidth specifies the minimum amount of space that argument a shall occupy. If a requires less space than fieldWidth, it is padded to fieldWidth with character fillChar. A positive fieldWidth produces right-aligned text. A negative fieldWidth produces left-aligned text.
    So, try
    Qt Code:
    1. arg(str1, -20)
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: QString and .arg()

    The documentation for QString::arg() specifies that if the fieldWidth argument is positive (which is the case in your example), then you will get right-aligned text. This is why spaces are inserted on the left.

    Also, do not use .arg().arg().arg()! Can you see what would happen if a student decided to put "%1" in their name? (BTW I just realized that the example in the docs makes this mistake.)

    I would use QString("%1 = %2").arg(name, value) or QString("%1 = %2").arg(QString("%1").arg(name, -20), value) for a left-aligned padded student name.

Similar Threads

  1. Replies: 3
    Last Post: 27th July 2012, 09:30
  2. Replies: 3
    Last Post: 13th February 2012, 04:16
  3. Replies: 2
    Last Post: 11th August 2011, 15:42
  4. Replies: 4
    Last Post: 1st February 2010, 14:21
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.