Results 1 to 4 of 4

Thread: Forming a tree of QWidgetItems

  1. #1
    Join Date
    Nov 2006
    Location
    New Malden, UK
    Posts
    11
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Forming a tree of QWidgetItems

    Hi everyone,

    I'm trying to use QTreeWidgetItem to display a hierarchy of categories for a blog, as is found in Wordpress. I thought I could create a QMap of QListWidgetItem pointers with the category ID as the key, then iterate through the list and add each item to its relevant parent, like so:


    Qt Code:
    1. // Now add all the categories into a big list
    2. for( b = 0; b < catsToDisplay; b++ ) {
    3. catItem = new QTreeWidgetItem;
    4. id = mainCatList.at( b ).firstChildElement( "categoryId" ).text();
    5. //qDebug() << "id =" << id;
    6. catItem->setText( 0, decodeXmlEntities( mainCatList.at( b ).firstChildElement( "categoryName" ).text() ) );
    7. description = decodeXmlEntities( mainCatList.at( b ).firstChildElement( "description" ).text() );
    8. if( !description.isEmpty() )
    9. catItem->setStatusTip( 0, description );
    10. catItem->setData( 0, Qt::UserRole, QVariant( id ) );
    11. catItem->setData( 0, 33, QVariant( false ) ); // i.e. not primary category until required
    12. catItems[id] = catItem;
    13. }
    14. qDebug() << "done the top-level categories";
    15. // Now assign each category to its respective parent, and make a list of
    16. // top-level items
    17. Q_FOREACH( QTreeWidgetItem *item, catItems ) {
    18. thisId = item->data( 0, Qt::UserRole ).toString();
    19. qDebug() << "now doing" << thisId;
    20. if( thisId != "0" && catItems.contains( thisId ) ) {
    21. qDebug() << "not a top-level";
    22. thisItem = catItems[thisId];
    23. qDebug() << "got item" << thisItem->data( 0, Qt::UserRole ).toString();
    24. Q_ASSERT( thisItem != 0 );
    25. thisItem->addChild( item );
    26. }
    27.  
    28. if( thisId == "0" ) {
    29. qDebug() << "top-level";
    30. topLevelCatItems.append( item );
    31. }
    32. qDebug() << "have now done" << thisId;
    33. }
    To copy to clipboard, switch view to plain text mode 

    The full method can be found here at my Bitbucket hg repo.

    The program halts at line 25 -- where it tries to add a child QTreeWidgetItem to one that doesn't have a parent. My aim in doing this was to build up a tree of items which could all be added, in one go, to the QTreeWidget, because it seems more efficient than having to run several recursive loops, and also because the user wouldn't have to watch the tree appear bit by bit.

    It doesn't crash or segfault; it just stops and you have to kill the process.

    Can anyone tell me what I'm doing wrong here? It doesn't say in the documentation that you can't add a child to a parentless item.

    Regards,

    Matt

  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: Forming a tree of QWidgetItems

    Hi Matthew,

    try running your app under control of a debugger and when it halts, break it (Ctrl+C in gdb) and print a backtrace. Make sure to have Qt debugging symbols available so that the debugger can look into Qt routines as well.
    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. #3
    Join Date
    Nov 2006
    Location
    New Malden, UK
    Posts
    11
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Forming a tree of QWidgetItems

    OK ... this is what I got when I used the debug info:


    Qt Code:
    1. Program received signal SIGINT, Interrupt.
    2. QTreeWidgetItemPrivate::propagateDisabled (this=<value optimized out>, item=0xb1e120)
    3. at itemviews/qtreewidget.cpp:1658
    4. 1658 itemviews/qtreewidget.cpp: No such file or directory.
    5. in itemviews/qtreewidget.cpp
    6. (gdb)
    7. (gdb) backtrace
    8. #0 QTreeWidgetItemPrivate::propagateDisabled (this=<value optimized out>, item=0xb1e120)
    9. at itemviews/qtreewidget.cpp:1658
    10. #1 0x00007ffff78994aa in QTreeWidgetItem::insertChild (this=0xb1e120, index=0, child=
    11. 0xb1e120) at itemviews/qtreewidget.cpp:1928
    12. #2 0x00007ffff78995fb in QTreeWidgetItem::addChild (this=0xb1e120, child=0xb1e120)
    13. at itemviews/qtreewidget.cpp:1882
    14. #3 0x000000000049fce8 in EditingWindow::wp_getCategories (this=0xb8d210, response=...)
    15. at /home/indigojo/hg/qtm-1.4/EditingWindow_ResponseHandlers.cc:600
    To copy to clipboard, switch view to plain text mode 
    Last edited by IndigoJo; 16th September 2010 at 21:28. Reason: Replaced quote tag with code

  4. #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: Forming a tree of QWidgetItems

    There is nothing in Qt's code that would cause a freeze.
    You could switch to frame #1 and print values of this->text() and child->text() to see what items are being processed. It could get you some insight whether the program indeed freezes at the elements you think it freezes at.

    I'm trying to analyze your algorithm but honestly I'm having a hard time understanding it.
    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.


Similar Threads

  1. Tree without using of QStandardItem
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 12th July 2010, 13:23
  2. Tree Widget
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 08:31
  3. Tree View
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2009, 07:31
  4. creating a Tree
    By mickey in forum General Programming
    Replies: 0
    Last Post: 20th April 2008, 18:57
  5. traverse a xml tree
    By mickey in forum General Programming
    Replies: 2
    Last Post: 27th January 2008, 13:46

Tags for this Thread

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.