PDA

View Full Version : Any Qt support for building strings from lists?



feraudyh
13th May 2010, 21:08
Hi,
I have a QList<QPointF> type object. I'd like to make a string out of it with commas between the string representations of the points. I wonder if there is a convenience method for this which avoids wasteful allocation of memory.

In C# there was a StringBuilder, if I recall right. I'm looking for something analogous.

tbscope
13th May 2010, 21:10
QTextStream

feraudyh
14th May 2010, 09:42
Hi,
The following code seems to work,


QString aString;
QTextStream out(&aString,QIODevice::WriteOnly);
out << 1;
out << "+";
out << 2;
// and so on...
qDebug()<<aString;
But I'd have to be convinced that it doesn't keep on concatenating and reallocating wastefully
Thanks anyway.

wysota
14th May 2010, 09:45
If you want to have a string then you have to allocate as much memory as needed to hold the string. If you just want to store the textual representation somewhere then you can use QTextStream as alreay suggested to save pieces of your text to a file. But regardless if you use the text stream or not, you'll have to create those pieces of text yourself.