PDA

View Full Version : QStringList



jaca
16th May 2008, 16:02
I wanted to copy a QStringList to another QStringList but only taking the odd positions.


QStringList pa[1] = QStringList im[0];
QStringList pa[3] = QStringList im[1];
QStringList pa[5] = QStringList im[2];
.
.
.

Someone has idea?
Thanks.

^NyAw^
16th May 2008, 16:25
Hi,

Use something similar to this:



int i=0;
int j=1;
while (i<im.count())
{
pa[j] = im[i];
i++;
j +=2;
}


Not tested

jaca
16th May 2008, 20:40
Hi,

Use something similar to this:



int i=0;
int j=1;
while (i<im.count())
{
pa[j] = im[i];
i++;
j +=2;
}


Not tested

tried in many ways, but always occurs Segmentation fault.


while (i < pa.count())
{
im[i] = pa[j];
i++;
j +=2;
}

there is no way in Qt?

wysota
16th May 2008, 20:43
You have to resize the list or use append() to insert items and the indexes of the list have to be a sequence. If you want only odd index numbers, use QMap.

int index = 1;
QStringList inlist;
QMap<int, QString> out;
foreach(const QString &str, inlist){
out[index] = str;
index+=2;
}

jaca
16th May 2008, 21:41
You have to resize the list or use append() to insert items and the indexes of the list have to be a sequence. If you want only odd index numbers, use QMap.

int index = 1;
QStringList inlist;
QMap<int, QString> out;
foreach(const QString &str, inlist){
out[index] = str;
index+=2;
}

tried this code:


QMap<int, QString>::const_iterator i = out.constBegin();
while (i != out.constEnd()) {
cout << i.key() << ": " << i.value() << endl;
++i;
}

Only I could not show values. As I see the values of QMap?
Thanks.

wysota
17th May 2008, 10:12
Do you have console support enabled? I suggest using QMessageBox instead of cout to be sure.