QTreeWidgetItem Problems



  • Hi,

    I'm trying create a QTreeWidgetList with multiple parents
    root->parent->another parent->item

    I'm adding the items by traversing through the already added parents and selecting the correct one by searching after the name of the parent. This works good until I try to add items to "another parent" then it returns null even if it have found correct parent node. Can anyone help me figure out what is happening?

    @

    QTreeWidgetItem* LabelingWidgetQt::iterateTreeWidget(QTreeWidgetItem* parent, QString str)
    {
    int count = parent ? parent->childCount() : treeWidget_->topLevelItemCount();

    for (int i = 0; i < count; i++)
    {
        QTreeWidgetItem* item = parent ? parent->child(i) : treeWidget_->topLevelItem(i);
        
        if(item->text(0)==str){
            return item;
        }
        
        iterateTreeWidget(item,str);
    }
    return 0;
    

    }
    @

    Here I create the Item and adds it to the found parent node.
    @ QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, QString::fromStdString(name));

    QTreeWidgetItem* parentItem = iterateTreeWidget(0,QString::fromStdString(parent));
    if(parentItem){
    parentItem->addChild(item);
    }@
    

    I'm using the same iterate method to add the parents/groups and this works fine and I can have multiple parents, but I cannot add items to the multiple parents, only to the topLevelItems.

    @ QTreeWidgetItem* groupItem;

    if(parent == "root"){
    groupItem = new QTreeWidgetItem(treeWidget_);
    groupItem->setText(0, QString::fromStdString(name));
    } else {
    QTreeWidgetItem* parentItem = iterateTreeWidget(0,QString::fromStdString(parent));
    groupItem = new QTreeWidgetItem(parentItem);
    groupItem->setText(0, QString::fromStdString(name));
    }@
    

    Thanks for any help!

    [EDIT: code indentation, Volker]



  • Problem is with your iterateTreeWidget() method:

    @
    QTreeWidgetItem* LabelingWidgetQt::iterateTreeWidget(QTreeWidgetItem* parent, QString str)
    {
    int count = parent ? parent->childCount() : treeWidget_->topLevelItemCount();

    for (int i = 0; i < count; i++)
    {
        QTreeWidgetItem* item = parent ? parent->child(i) : treeWidget_->topLevelItem(i);
        
        if(item->text(0)==str){
            return item;
        }
        
        iterateTreeWidget(item,str);
    }
    return 0;
    

    }
    @

    In line 13 you must check if your call to iterateTreeWidget was successful! You do not and enter just the next index of your for loop! Change your method to this one:

    @
    QTreeWidgetItem* LabelingWidgetQt::iterateTreeWidget(QTreeWidgetItem* parent, QString str)
    {
    int count = parent ? parent->childCount() : treeWidget_->topLevelItemCount();

    for (int i = 0; i < count; i++)
    {
        QTreeWidgetItem* item = parent ? parent->child(i) : treeWidget_->topLevelItem(i);
        
        if(item->text(0)==str){
            return item;
        }
        
        QTreeWidgetItem *resultItem = iterateTreeWidget(item,str);
        if(resultItem)
            return resultItem;
    }
    return 0;
    

    }
    @



  • Thank you very much! :) I was staring hours on my code, so I must have gone blind. ;)
    I should have posted my problem earlier, damn that was annoying! Weird that the compiler did not complain...



  • The compiler cannot complain. You do not even have endless recursion here :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.