Thread: How to Convert a QStringList into an array of doubles??

Hybrid View

1. Hi everyone!

Can any body tell me How to Convert a QStringList into an array of doubles???

e.g. I have a QStringList: 1 2 3 4 5 6 7 8 1 9
The Code that I've tried is as under:

QString str = "1 2 3 4 5 6 7 8 1 9";
QList<QString> list;
list = str.split(QRegExp("\\s+"),QString::SkipEmptyParts);

double n[list.size()];

QList<QString>::iterator i;
int j; QString temp;
for(i = list.begin(); i != list.end(); ++i)
{
temp = *i;
j = list.indexOf(temp);
n[j] = temp.toDouble();
}

for(int j=0;j<list.size();j++)
qDebug()<<n[j];

But the out put is as follows:
1
2
3
4
5
6
7
8
6.952217e-308
9

I've tried java style iterator (while(i.hasNext)............) as well but same result..

2. what would you like to achieve by this?

you can also try like QList<double> or check for QMap also. it may he simple and helpful for you.

3. I wanna use this in gui application. in which a user enters a list of doubles. I want to convert that text into an array of doubles for further processing.

4. Hi,

for(i = list.begin(); i != list.end(); ++i)
{
temp = *i;
j = list.indexOf(temp); // here indexOf returns always the first occurance of the item (temp here), so incase there are duplicates, this logic wont work.. hence its not working..
n[j] = temp.toDouble();
}

So just use this and try ,

for(j=0, i = list.begin(); i != list.end(); ++i, j++)
{
temp = *i;

n[j] = temp.toDouble();
}

5. Thanks kusumk!!
It works..

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.