PDA

View Full Version : Problem inserting items inside TreeWidget



hakermania
3rd May 2011, 18:57
Hello guys, I have a problem inserting items to a tree widget!

Some info about the (attached) screenshot now, 'list' is declared as
QList<QTreeWidgetItem*> list;
Also,

qDebug() << parnt->text(i-2);
list.insert(i-2,parnt);
are inside a for loop, while

ui->treeWidget->addTopLevelItems(list) is outside it.
When I output the parnt at 'i-2' as you can see, it does echo

2011 May
2011 October

At the exact next line I insert the parnt to the 'list' at 'i-2' (the exact point I echoed parnt to the previous line)

But as you can see in the window besides it, it inserts '2011 May' 2 times, and I don't know why!

Also, mention that

insert.at(i-2)->addChild/addChilds(parnt);
crashes my application, so I cannot use them (I don't know why).

Thx in advance for any answer/help!:)6357

wysota
4th May 2011, 00:41
It's not possible to determine what's wrong here with such incomplete snippets of code. I can only advise you to print the contents of "list" before you add it to the list widget.

hakermania
4th May 2011, 13:47
Ok, sorry for being difficult to understand, the code is this:

for(int i=2; i < a; i++){
//code here...
for(int j=2;j<dir1_count;j++){
//code here...
//here adding a child to parnt...
parnt->addChild(child1);
}
//after adding the children to the parnt I insert the parnt to the list
qDebug() << parnt->text(i-2);
list.insert(i-2,parnt);
}
//finally, i insert the list to the treewidget...
ui->treeWidget->addTopLevelItems(list);
The output of qDebug() as I said to the first post is as I expected to be (different month each time) but in the treewidget there is inserted the same month (the first one) without an obvious reason. This is the same code but it also outputs the list's contents:

qDebug() << "Parnt at " << i-2 << " has '" << parnt->text(i-2) << "'";
list.insert(i-2,parnt);
qDebug() << " List at " << i-2 << " has '" << list.at(i-2)->text(0) << "'";
So, by running this code by outputing the contents of the 'list', the output is:

Parnt at 0 has ' "2011 May" ' <- As expected
List at 0 has ' "2011 May" ' <- As expected
Parnt at 1 has ' "2011 October" ' <- As expected
List at 1 has ' "2011 May" ' <- No comment :/
Which i find weird...
Obviously, the error is while inserting the 'parnt' into the 'list' or while using addTopLevelItems()

Thx in advance again...

wysota
4th May 2011, 14:55
What is child1? How do you initialize this variable? And why are you iterating i from 2 and not from 0?

hakermania
4th May 2011, 15:30
The code is like this:

QTreeWidgetItem *child1 = new QTreeWidgetItem;
QDir his_file;
.........
his_file.setPath(QString(subdir1.path() + "/" + subdir1.entryList().at(j)));
child1->setText(0,his_file.dirName());
parnt->addChild(child1);
So, it starts from 2 in order to avoid the paths at 0 and 1 (which means the paths: ) . (current) and .. (one previous), because I actually read folders' names and I add them to parnt, then add parnt to list and list to treewidget...

wysota
4th May 2011, 15:54
Why don't you ask QDir to skip those special entries then?

hakermania
4th May 2011, 16:45
Thx for the suggestion but my solution at that point works perfectly and there's no point in changing it right now.
In the point now, I don't now what the point(problem) is xD

wysota
4th May 2011, 16:58
I think there is a point in changing it right now, because due to increased complexity of your code you can't be sure it is not the value of "i" that is causing the problem.

hakermania
4th May 2011, 17:47
I'm sure that this isn't the problem! In the specific example it is clear that
i-2 at the first time the for loop runs equals to 0 and the 2nd time equals to 1 (see the lines with the qDebug() I've made that outputs the i-2)
So, I'm pretty sure that this isn't the problem, trust me :PPP

Added after 25 minutes:

And here's a compilable example that does exactly the same as what I want to do and it still fails(see attachment)

6363

wysota
4th May 2011, 17:52
I'm sure that this isn't the problem! In the specific example it is clear that
i-2 at the first time the for loop runs equals to 0 and the 2nd time equals to 1 (see the lines with the qDebug() I've made that outputs the i-2)
That's not the problem. The problem might be ending the loop prematurely or something like that. The only explanation for having two same entries in one of the lists is that you insert the same data twice into the list and that involves iterating the other list. So make sure the iterating code is as simple as possible. Also the reason for addChild() crashing is probably an invalid pointer as the callee which is calculated based on your "i-2" iterator.

You use a lot of strange variables and you haven't shown us what they contain. So either show us complete code including initialization of all the variables you use or get rid of those variables completely. Or better yet prepare a minimal compilable example reproducing the problem.

hakermania
4th May 2011, 18:05
Look at my previous reply, I have provided a compilable example :)

wysota
4th May 2011, 18:17
Well... you are inserting the same element into the list twice so why are you surprised you get it twice in the list? Increase the number of columns in your tree widget and you'll see what the problem is.

hakermania
4th May 2011, 18:36
Yep, you're right :/
But every 'parnt' has diferent text() and different children, any possible solution so as to insert into list correctly every parent exiting the for loop?

wysota
4th May 2011, 18:44
Create the item inside the loop.

hakermania
4th May 2011, 18:57
yeah, that's what i thought of doing, too ;)
Thx again