# How to Convert a QStringList into an array of doubles??

• 2012-04-02, 06:19
engrnaveed
How to Convert a QStringList into an array of doubles??
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..
• 2012-04-02, 06:25
kkrish
Re: How to Convert a QStringList into an array of doubles??
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.
• 2012-04-02, 06:46
engrnaveed
Re: How to Convert a QStringList into an array of doubles??
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.
• 2012-04-02, 07:49
kusumk
Re: How to Convert a QStringList into an array of doubles??
Hi,

for(i = list.begin(); i != list.end(); ++i)
{
temp = *i;
[B]j = list.indexOf(temp);[/B] // 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();
}
• 2012-04-03, 04:49
engrnaveed
Re: How to Convert a QStringList into an array of doubles??
Thanks kusumk!!
It works..