View Full Version : Problem inserting items inside TreeWidget

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;

qDebug() << parnt->text(i-2);
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

crashes my application, so I cannot use them (I don't know why).

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

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.

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...
//after adding the children to the parnt I insert the parnt to the list
qDebug() << parnt->text(i-2);
//finally, i insert the list to the treewidget...
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) << "'";
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...

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?

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)));
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...

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

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

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.

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)


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.

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

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.

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?

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

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