PDA

View Full Version : String operations, printing to stdout



Cruz
19th January 2009, 14:15
I'm not very fit with the QString class. I need to build a string representation of data which is a mixture of text and data. Is there a more elegant way than doing this?



QString string;
QString temp;
QList<float> angles;

string = "data starts here: ";
for (int i = 0; i <angles->size(); i++)
{
temp.sprintf("\t%.2f", angles.at(i));
string.append(temp);
}


And then what's the best way to print this string to stdout? All I know is printf from C and I can't get that to work with Qstring.

Thanks
Cruz

wysota
19th January 2009, 14:32
Try something like this:

#include <QtDebug>
//...
QString string("data starts here: ");
for(int i=0;i<angles->size();++i)
string+=QString::number(angles.at(i), 'f', 2);
qDebug() << string;

spirit
19th January 2009, 14:35
#include <iostream>
...
QString sometext = "Some text ";
std::cout << sometext.toLocal8Bit().constData() << 1 << " " << 1.3;
...

or I don't understand? :)

jpn
20th January 2009, 15:30
Even


qDebug() << angles;

works.