Results 1 to 15 of 15

Thread: Problem inserting items inside TreeWidget

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem inserting items inside TreeWidget

    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,
    Qt Code:
    1. qDebug() << parnt->text(i-2);
    2. list.insert(i-2,parnt);
    To copy to clipboard, switch view to plain text mode 
    are inside a for loop, while
    Qt Code:
    1. ui->treeWidget->addTopLevelItems(list)
    To copy to clipboard, switch view to plain text mode 
    is outside it.
    When I output the parnt at 'i-2' as you can see, it does echo
    Qt Code:
    1. 2011 May
    2. 2011 October
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. insert.at(i-2)->addChild/addChilds(parnt);
    To copy to clipboard, switch view to plain text mode 
    crashes my application, so I cannot use them (I don't know why).

    Thx in advance for any answer/help!error.png
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    Ok, sorry for being difficult to understand, the code is this:
    Qt Code:
    1. for(int i=2; i < a; i++){
    2. //code here...
    3. for(int j=2;j<dir1_count;j++){
    4. //code here...
    5. //here adding a child to parnt...
    6. parnt->addChild(child1);
    7. }
    8. //after adding the children to the parnt I insert the parnt to the list
    9. qDebug() << parnt->text(i-2);
    10. list.insert(i-2,parnt);
    11. }
    12. //finally, i insert the list to the treewidget...
    13. ui->treeWidget->addTopLevelItems(list);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. qDebug() << "Parnt at " << i-2 << " has '" << parnt->text(i-2) << "'";
    2. list.insert(i-2,parnt);
    3. qDebug() << " List at " << i-2 << " has '" << list.at(i-2)->text(0) << "'";
    To copy to clipboard, switch view to plain text mode 
    So, by running this code by outputing the contents of the 'list', the output is:
    Qt Code:
    1. Parnt at 0 has ' "2011 May" ' <- As expected
    2. List at 0 has ' "2011 May" ' <- As expected
    3. Parnt at 1 has ' "2011 October" ' <- As expected
    4. List at 1 has ' "2011 May" ' <- No comment :/
    To copy to clipboard, switch view to plain text mode 
    Which i find weird...
    Obviously, the error is while inserting the 'parnt' into the 'list' or while using addTopLevelItems()

    Thx in advance again...
    Last edited by hakermania; 4th May 2011 at 17:32.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    What is child1? How do you initialize this variable? And why are you iterating i from 2 and not from 0?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  7. #5
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    The code is like this:
    Qt Code:
    1. QDir his_file;
    2. .........
    3. his_file.setPath(QString(subdir1.path() + "/" + subdir1.entryList().at(j)));
    4. child1->setText(0,his_file.dirName());
    5. parnt->addChild(child1);
    To copy to clipboard, switch view to plain text mode 
    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...
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    Why don't you ask QDir to skip those special entries then?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  10. #7
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    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
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  13. #9
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    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)

    test_wysota.zip
    Last edited by hakermania; 4th May 2011 at 17:47.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    Quote Originally Posted by hakermania View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  16. #11
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    Look at my previous reply, I have provided a compilable example
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  17. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  19. #13
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    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?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  20. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem inserting items inside TreeWidget

    Create the item inside the loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. The following user says thank you to wysota for this useful post:

    hakermania (4th May 2011)

  22. #15
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem inserting items inside TreeWidget

    yeah, that's what i thought of doing, too
    Thx again
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

Similar Threads

  1. Inserting label inside textEdit widget
    By dshan in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2011, 11:17
  2. add items to treewidget
    By Shien in forum Qt Programming
    Replies: 15
    Last Post: 1st January 2011, 19:40
  3. Problem editing TreeWidget items
    By Moezzie in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2007, 21:22
  4. Replies: 2
    Last Post: 29th May 2007, 13:55
  5. Problem inserting child items into a QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2006, 18:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.