PDA

View Full Version : QList<QList > question



SubV
25th July 2008, 11:48
Hello all,

I'm trying to create a 2-dimensional QList of some structure.

Here's the test code (contents of condition struct isn't important, both stateDWord and zone_id variables are int):

taskData td2;
QList<condition_struct*> *conditions2 = new QList<condition_struct*>;
QList<QList<condition_struct*>* > *tasks2 = new QList<QList<condition_struct*>* >;

condition_struct *cnd2 = new condition_struct;
cnd2->stateDWord = 1;
cnd2->td_current.zone_id = 2;
conditions2->push_back(cnd2);
cnd2 = new condition_struct;
cnd2->stateDWord = 3;
cnd2->td_current.zone_id = 4;
conditions2->push_back(cnd2);

tasks2->push_back(conditions2);

cnd2 = new condition_struct;
cnd2->stateDWord = 5;
cnd2->td_current.zone_id = 6;
conditions2->push_back(cnd2);
cnd2 = new condition_struct;
cnd2->stateDWord = 7;
cnd2->td_current.zone_id = 8;
conditions2->push_back(cnd2);
cnd2 = new condition_struct;
cnd2->stateDWord = 9;
cnd2->td_current.zone_id = 10;
conditions2->push_back(cnd2);

tasks2->push_back(conditions2);

//...

condition_struct *cndCr = new condition_struct;

QList<condition_struct*> *conditionsCr = new QList<condition_struct*>;

printf("tasks2 vector size: %d\n\n",tasks2->size());
for(QList<QList<condition_struct*>* >::iterator ti0=tasks2->begin();ti0!=tasks2->end();ti0++)
{
printf("conditions2 vector size: %d\n\n", (*ti0)->size());
for(QList<condition_struct*>::iterator ci0=(*ti0)->begin();ci0!=(*ti0)->end();ci0++)
{
cndCr->stateDWord = (*ci0)->stateDWord;
cndCr->td_current.zone_id = (*ci0)->td_current.zone_id;
printf("stateDWord: %d, zone_id: %d\n",cndCr->stateDWord,cndCr->td_current.zone_id);
(*ci0)->stateDWord = cndCr->stateDWord;
(*ci0)->td_current.zone_id = cndCr->td_current.zone_id;
}
}


When running this code, I got the following output:

tasks2 vector size: 2

conditions2 vector size: 5

stateDWord: 1, zone_id: 2
stateDWord: 3, zone_id: 4
stateDWord: 5, zone_id: 6
stateDWord: 7, zone_id: 8
stateDWord: 9, zone_id: 10

conditions2 vector size: 5

stateDWord: 1, zone_id: 2
stateDWord: 3, zone_id: 4
stateDWord: 5, zone_id: 6
stateDWord: 7, zone_id: 8
stateDWord: 9, zone_id: 10

instead of something like

tasks2 vector size: 2

conditions2 vector size: 2

stateDWord: 1, zone_id: 2
stateDWord: 3, zone_id: 4

conditions2 vector size: 3

stateDWord: 5, zone_id: 6
stateDWord: 7, zone_id: 8
stateDWord: 9, zone_id: 10


What am I doing wrong? Thank you in advance for the answer.

SubV
25th July 2008, 12:08
Already found the answer. I forget to initialize another copy of structure before pushing it to the list.

taskData td2;
QList<condition_struct*> *conditions2 = new QList<condition_struct*>;
QList<QList<condition_struct*>* > *tasks2 = new QList<QList<condition_struct*>* >;

condition_struct *cnd2 = new condition_struct;
cnd2->stateDWord = 1;
cnd2->td_current.zone_id = 2;
conditions2->push_back(cnd2);
cnd2 = new condition_struct;
cnd2->stateDWord = 3;
cnd2->td_current.zone_id = 4;
conditions2->push_back(cnd2);

tasks2->push_back(conditions2);

>>>>> conditions2 = new QList<condition_struct*>;

jpn
25th July 2008, 13:52
I'd recommend not to allocate QLists on the heap in the first place because it's an implicitly shared class (http://doc.trolltech.com/4.4/shared.html). Allocating it on the stack simplifies your code and makes it much less error prone.

SubV
25th July 2008, 15:14
jpn

Thank you for your advice, I'll try this.