IndigoJo
16th September 2010, 09:44
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:
// Now add all the categories into a big list
for( b = 0; b < catsToDisplay; b++ ) {
catItem = new QTreeWidgetItem;
id = mainCatList.at( b ).firstChildElement( "categoryId" ).text();
//qDebug() << "id =" << id;
catItem->setText( 0, decodeXmlEntities( mainCatList.at( b ).firstChildElement( "categoryName" ).text() ) );
description = decodeXmlEntities( mainCatList.at( b ).firstChildElement( "description" ).text() );
if( !description.isEmpty() )
catItem->setStatusTip( 0, description );
catItem->setData( 0, Qt::UserRole, QVariant( id ) );
catItem->setData( 0, 33, QVariant( false ) ); // i.e. not primary category until required
catItems[id] = catItem;
}
qDebug() << "done the top-level categories";
// Now assign each category to its respective parent, and make a list of
// top-level items
Q_FOREACH( QTreeWidgetItem *item, catItems ) {
thisId = item->data( 0, Qt::UserRole ).toString();
qDebug() << "now doing" << thisId;
if( thisId != "0" && catItems.contains( thisId ) ) {
qDebug() << "not a top-level";
thisItem = catItems[thisId];
qDebug() << "got item" << thisItem->data( 0, Qt::UserRole ).toString();
Q_ASSERT( thisItem != 0 );
thisItem->addChild( item );
}
if( thisId == "0" ) {
qDebug() << "top-level";
topLevelCatItems.append( item );
}
qDebug() << "have now done" << thisId;
}
The full method can be found here (http://bitbucket.org/IndigoJo/qtm-1x/src/b2da4958862e/EditingWindow_ResponseHandlers.cc) 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
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:
// Now add all the categories into a big list
for( b = 0; b < catsToDisplay; b++ ) {
catItem = new QTreeWidgetItem;
id = mainCatList.at( b ).firstChildElement( "categoryId" ).text();
//qDebug() << "id =" << id;
catItem->setText( 0, decodeXmlEntities( mainCatList.at( b ).firstChildElement( "categoryName" ).text() ) );
description = decodeXmlEntities( mainCatList.at( b ).firstChildElement( "description" ).text() );
if( !description.isEmpty() )
catItem->setStatusTip( 0, description );
catItem->setData( 0, Qt::UserRole, QVariant( id ) );
catItem->setData( 0, 33, QVariant( false ) ); // i.e. not primary category until required
catItems[id] = catItem;
}
qDebug() << "done the top-level categories";
// Now assign each category to its respective parent, and make a list of
// top-level items
Q_FOREACH( QTreeWidgetItem *item, catItems ) {
thisId = item->data( 0, Qt::UserRole ).toString();
qDebug() << "now doing" << thisId;
if( thisId != "0" && catItems.contains( thisId ) ) {
qDebug() << "not a top-level";
thisItem = catItems[thisId];
qDebug() << "got item" << thisItem->data( 0, Qt::UserRole ).toString();
Q_ASSERT( thisItem != 0 );
thisItem->addChild( item );
}
if( thisId == "0" ) {
qDebug() << "top-level";
topLevelCatItems.append( item );
}
qDebug() << "have now done" << thisId;
}
The full method can be found here (http://bitbucket.org/IndigoJo/qtm-1x/src/b2da4958862e/EditingWindow_ResponseHandlers.cc) 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