PDA

View Full Version : how convert numbers into text



tommy
3rd August 2009, 15:39
How do you convert numbers into text for QString.
For example, I want to build a word 2house4 using append(). I know how to make such a string but I want the string to be all text (the numbers should also be treated as text):



QString test;
test.append(2);
test.append("house");
test.append(4);


This doesn't work:


QString test;
test.append(2).text();
test.append("house");
test.append(4).text();


thanks

spirit
3rd August 2009, 15:56
use QString::number or QLocale::toString.

jpujolf
5th August 2009, 08:55
How do you convert numbers into text for QString.
For example, I want to build a word 2house4 using append(). I know how to make such a string but I want the string to be all text (the numbers should also be treated as text):



QString test;
test.append(2);
test.append("house");
test.append(4);


This doesn't work:


QString test;
test.append(2).text();
test.append("house");
test.append(4).text();


thanks
You can do it like this :


QString test;
test = QString ( "%1%2%3" ).arg ( 2 ).arg ( "house" ).arg ( 4 );

If all three paarams where strings ( up to 10 ), you can use alternative syntax :


test = QString ( "%1%2%3" ).arg ( "2", "house", "4" );

schall_l
5th August 2009, 09:58
or simply do something like:


QString test;
test.append( QString(%1).arg(2) );
test.append("house");
test.append( QString(%1).arg(4) );

that could also be:


int a;
int b;

a = 2;
b = 4;

QString test;
test.append( QString(%1).arg(a) );
test.append("house");
test.append( QString(%1).arg(b) );

spirit
5th August 2009, 10:01
you missed quotes in your example


QString(%1)

should be


QString("%1")

:)