Results 1 to 13 of 13

Thread: QString and .arg()

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

    Default QString and .arg()

    I want to format a string by adding spaces using .arg, I used

    QString(%1 %2 %3).arg(name,20).arg(" = ").arg(value);


    the result i expect:

    "Student Name = Jemes"

    but I got the next , it left spaces before the name not after:

    " Student Name = Jemes"


    how i can make is start from the beginning and leave all the space after not before.


    Thank you

  2. #2
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString and .arg()

    It *also* left that after from what I can tell. This just means you have a space at the beginning of your "name" string. I can't imagine that's not the case.

    By that I mean you defined name as QString(" Student Name"); or something. I think you probably just didn't notice

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

    Default Re: QString and .arg()

    i think what you mean that its me who have the space in my paramter as " Student Name" ... No, im so sure that i do not have any spaces in the parameters.

    if this the case even if i use QString(%1 %2 %3).arg(name).arg(" = ").arg(value); Not QString(%1 %2 %3).arg(name,20).arg(" = ").arg(value); with out the "20" as a width
    the result will be the same , but this is not the case. if i don't put any width the result will be

    "Studen Name = Jeems"

    * mean a space
    the editor remove all the spaces

    Student Name********= Jemms <=== this is what i want

    ********Student Name = Jemms <=== this is what i got.

    and I assure that i don't have any spaces in the parameters that holds the "names".

  4. #4
    Join Date
    Jun 2012
    Posts
    98
    Thanks
    11
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QString and .arg()

    I can't imagine it's ".arg" doing it. Go ahead and attach or paste your code( though I probably won't see it until tomorrow)

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

    Default Re: QString and .arg()

    here is an example of the code .. you can try it your self and see the result

    Qt Code:
    1. QString str1,str2,str3;
    2. str1="Student Name";
    3. str2= "James";
    4.  
    5. str3= QString("%1 %2 %3").arg(str1,20).arg(" = ").arg(str2);
    6. qDebug() << str3;
    To copy to clipboard, switch view to plain text mode 

    The result will be :
    "********Student Name = James"

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

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

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

    Default Re: QString and .arg()

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

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

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

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

    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

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

    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

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

    Default Re: QString and .arg()

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

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

    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.

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

    Default Re: QString and .arg()

    Thanks, Oleg Shparber, Spirit and tescrin for your reply and help


    Added after 15 minutes:


    Thanks yeye_olive for this advise. good point that i wasn't giving attention to
    Last edited by jesse_mark; 2nd August 2012 at 15:33.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.