View Full Version : QString and .arg()
jesse_mark
1st August 2012, 23:21
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
tescrin
1st August 2012, 23:31
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
jesse_mark
1st August 2012, 23:52
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".
tescrin
1st August 2012, 23:53
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)
jesse_mark
2nd August 2012, 14:42
here is an example of the code .. you can try it your self and see the result
QString str1,str2,str3;
str1="Student Name";
str2= "James";
str3= QString("%1 %2 %3").arg(str1,20).arg(" = ").arg(str2);
qDebug() << str3;
The result will be :
"********Student Name = James"
spirit
2nd August 2012, 14:59
What's your Qt's version? This code works fine for me.
jesse_mark
2nd August 2012, 15:04
it is Qt 4.8.1 ... you mean you got the spaces after the name like
"Student Name******** = jemes" ??
spirit
2nd August 2012, 15:07
I'm also using 4.8.1 and I've got such output:
int main(int, char**) " Student Name = James"
Oleg
2nd August 2012, 15:09
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
arg(str1, -20)
jesse_mark
2nd August 2012, 15:12
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
spirit
2nd August 2012, 15:16
see Oleg's post above. :)
yeye_olive
2nd August 2012, 15:16
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.
jesse_mark
2nd August 2012, 15:33
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.