Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidgetItem Problems
QtWS25 Last Chance

QTreeWidgetItem Problems

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.0k Views
  • 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 Offline
    R Offline
    rulfen
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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
      0
      • R Offline
        R Offline
        rulfen
        wrote on last edited by
        #3

        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
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved