Results 1 to 15 of 15

Thread: Problem inserting items inside TreeWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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)

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
  •  
Qt is a trademark of The Qt Company.