Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    QTreeWidgetItem Problems

    General and Desktop
    2
    4
    2738
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rulfen last edited by

      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]

      1 Reply Last reply Reply Quote 0
      • G
        goetz last edited by

        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;
        

        }
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • R
          rulfen last edited by

          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...

          1 Reply Last reply Reply Quote 0
          • G
            goetz last edited by

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

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply Reply Quote 0
            • First post
              Last post