Problem with QString .arg()
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
Re: Problem with QString .arg()
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.
Re: Problem with QString .arg()
Quote:
Originally Posted by
ChrisN
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:
Code:
dT.append("Force = %1 +/- %2\n".arg(F,0,'f',0).arg(tolF,0,'f',0));
or perhaps
Code:
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.)
Re: Problem with QString .arg()
Quote:
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.
Re: Problem with QString .arg()
Thank you all!
dT.append(QString("Force = %1 +/- %2\n").arg(F,0,'f',0).arg(tolF,0,'f',0));
does it.