PDA

View Full Version : convert each element of QVector<double> to QString and save in a text file



Alex22
10th February 2016, 19:18
Hi,
there is a QVector<double> in name of "v1". Size of "v1" is 1000. I need to convert all this 1000 elements to QString (or convert to a QStringList) and then save this list into a text file. Please help me in this way.

thanks

Lesiok
10th February 2016, 19:28
Read about QString::number

anda_skoa
10th February 2016, 19:46
If you want to save directly into the file, see QTextStream.

Cheers,
_

Alex22
10th February 2016, 19:47
Read about QString::number

thanks Lesiok,
I used this:


for(int y=0; y<v1.size(); y++)
{
qDebug()<< QString("%1").arg(v1.at(y));
}


how could i save this in a file (text type) in a one column?

ChrisW67
10th February 2016, 20:09
See anda_skoa's post above.

Alex22
11th February 2016, 06:43
Thanks all
I used this and works well:


QStringList list;
for(int y=0; y<v1.size(); y++)
{
list<< QString("%1").arg(v1.at(y));
}


QFile g1f;
g1f.setFileName("test.txt");
if(!g1f.open(QFile::WriteOnly | QFile::Text))
{
qDebug()<<"can not open to write";
}

QTextStream df(&g1f);
for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
df << *it << "\n";

g1f.flush();
g1f.close();


is there an optimal way to convert QVector<double> to QStringList? can we do this by QVector::toList()?

anda_skoa
11th February 2016, 09:09
The most optimal way is to not create the string list.

Cheers,
_