
Originally Posted by
nileshsince1980
Hi,
Thanks for reply ...
As I want to use std::setw(..) function for formatting and return the std::string object which will be then covert to QString using fromStdString(..).
Can you tell me how to use QString::arg(..) function for above code??
Check the docs! http://qt.nokia.com/doc/4.6/qstring.html#arg
Consider this code
ss << std::left << std::setw(40) << "Values" << std::setw(40) << "Keyword/Constant" << std::endl;
ss << std::left << std::setw(40) << "Values" << std::setw(40) << "Keyword/Constant" << std::endl;
To copy to clipboard, switch view to plain text mode
This will likely work with
QString("%1%2\n").
arg("Values",
-40).
arg("Keyword/Constant",
-40)
QString("%1%2\n").arg("Values", -40).arg("Keyword/Constant", -40)
To copy to clipboard, switch view to plain text mode
As a side note, if you desire to do this in a similar way using QT constructs, you can use a QTextSTream
http://qt.nokia.com/doc/4.6/qtextstream.html
Points of interest are:
- setFieldAlignment Sets the field alignment mode (so you can set left, center, right...). Use the left() shortcut.
- setFieldWidth Sets the current field width to width. This also applies to endl, so be careful.
OK, now for an untested example:
ss.setFieldWidth(40);
...
QString s;
QTextStream ss(&s);
ss.setFieldAlignment(QTextStream::AlignLeft);
ss.setFieldWidth(40);
...
To copy to clipboard, switch view to plain text mode
You can also use the inline functions:
ss << left << qSetFieldWidth(40) << "Value" << "Keyword/Constant" << qSetFieldWidth(0) << endl;
ss << qSetFieldWidth(40) << "One" << "One" << qSetFieldWidth(0) << endl;
qDebug(qPrintable(s));
QString s;
QTextStream ss(&s);
ss << left << qSetFieldWidth(40) << "Value" << "Keyword/Constant" << qSetFieldWidth(0) << endl;
ss << qSetFieldWidth(40) << "One" << "One" << qSetFieldWidth(0) << endl;
qDebug(qPrintable(s));
To copy to clipboard, switch view to plain text mode
I will note, however, that printing the last item on a line with a field width of 40 seems a little silly, because this simply provides spaces after the field, but before the new line character.
Bookmarks