View Full Version : Problem with QString .arg()
ChrisN
29th May 2011, 23:27
Can anyone tell me the error within the following lines:
QString dT;
dT.clear();
dT.append("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0);
returns
Force = 10 +/- 2
properly on stdout. But when writing to formatted file using
QFile file( repfname );
QTextStream ts( &file );
const char *foo = dT.toStdString().c_str();
ts << foo << endl;
file.close();
file contains
Force = %1 +/- %2
wysota
29th May 2011, 23:50
QString::arg() returns a modified copy of the string, it doesn't modify the string in place so you have to assign the result to some variable.
Can anyone tell me the error within the following lines:
QString dT;
dT.clear();
dT.append("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0);
returns
Force = 10 +/- 2
properly on stdout.
I suspect you are wrong there. What you tested was probably slightly different code. I believe this would work:
dT.append("Force = %1 +/- %2\n".arg(F,0,'f',0).arg(tolF,0,'f',0)); or perhaps
dT.append(QString("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0));
(I have trouble predicting when Qt is going to allow auto-conversion of C strings to QStrings.)
ChrisW67
30th May 2011, 08:30
I have trouble predicting when Qt is going to allow auto-conversion of C strings to QStrings. This a function of C++ and nothing to do with Qt. If the compiler cannot make sense of a parameter as written it will try a single type conversion, using a non-explicit conversion constructor for the allowable classes in that parameter position, and try to make sense of the result. It will never do more than one type conversion and will fail if there is more than one possible option.
Your first example fails because the constant C string has no associated class and hence no arg() method. It is not a parameter in its own right so no attempt at conversion is made.
I am absolutely certain that I have explained this poorly and missed nuances.
ChrisN
30th May 2011, 21:13
Thank you all!
dT.append(QString("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0));
does it.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.