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. QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once
QtWS25 Last Chance

QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 3.9k 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.
  • Taz742T Taz742

    @jsulm
    when I click on the button I can see treewidgetitems, then I close tab and click on this button again but this treewidgetitems is not shown.

    jsulmJ Online
    jsulmJ Online
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #9

    @Taz742 said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

    then I close tab

    I guess if you close the tab then the tab and its content (tree view) are deleted?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Taz742T 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Taz742 said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

      then I close tab

      I guess if you close the tab then the tab and its content (tree view) are deleted?

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #10

      @jsulm
      First click on my button
      alt text

      Result is good, i have 2 treeitem:
      alt text

      now close this tab and click again my button and insert tab, result is:
      alt text

      Do what you want.

      jsulmJ 1 Reply Last reply
      0
      • Taz742T Taz742

        @jsulm
        First click on my button
        alt text

        Result is good, i have 2 treeitem:
        alt text

        now close this tab and click again my button and insert tab, result is:
        alt text

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #11

        @Taz742 You should debug your app to see what happens.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Taz742T 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Taz742 You should debug your app to see what happens.

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #12

          @jsulm
          Of course i tryed...

              for (auto it = items.begin(); it != items.end(); it++) {
                  ui->treeWidget->addTopLevelItem(it.value());
                  it.value()->setExpanded(true);
              }
          

          Its entered in my loop, everything is done without a problem but i can't see my treeitems.
          Also i tryed

          qDebug() << ui->treeWidget->topLevelItemsCount();
          

          after end loop, and result is 0.

          Do what you want.

          1 Reply Last reply
          0
          • GeekOwLG Offline
            GeekOwLG Offline
            GeekOwL
            wrote on last edited by
            #13

            Here are you sure your wdgTemplate is nullptr after closing it ? or when close is activated are you sure you handled to not delete the closed tab and its data ?

             if (wdgTemplate) {
                    ui->tabWidget->setCurrentWidget(wdgTemplate);
                    return;
                }
            
            Taz742T 1 Reply Last reply
            0
            • GeekOwLG GeekOwL

              Here are you sure your wdgTemplate is nullptr after closing it ? or when close is activated are you sure you handled to not delete the closed tab and its data ?

               if (wdgTemplate) {
                      ui->tabWidget->setCurrentWidget(wdgTemplate);
                      return;
                  }
              
              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by Taz742
              #14

              @GeekOwL said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

              Here are you sure your wdgTemplate is nullptr after closing it ?

              Yes.

                  QObject::connect(this->ui->tabWidget, &QTabWidget::tabCloseRequested, this, [=](int index) {
                      if (index != ui->tabWidget->indexOf(dlg)) {
                          if(ui->tabWidget->widget(index) == dlgRep) {
                              ui->tabWidget->removeTab(index);
                              dlgRep = NULL;
                          } else if (ui->tabWidget->widget(index) == wdgTemplate) {
                              ui->tabWidget->removeTab(index);
                              wdgTemplate = NULL;
                          }
                      }
                  });
              

              @GeekOwL said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

              or when close is activated are you sure you handled to not delete the closed tab and its data

              I dont know... I have no answer to this question.

              Do what you want.

              Taz742T 1 Reply Last reply
              0
              • Taz742T Taz742

                @GeekOwL said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

                Here are you sure your wdgTemplate is nullptr after closing it ?

                Yes.

                    QObject::connect(this->ui->tabWidget, &QTabWidget::tabCloseRequested, this, [=](int index) {
                        if (index != ui->tabWidget->indexOf(dlg)) {
                            if(ui->tabWidget->widget(index) == dlgRep) {
                                ui->tabWidget->removeTab(index);
                                dlgRep = NULL;
                            } else if (ui->tabWidget->widget(index) == wdgTemplate) {
                                ui->tabWidget->removeTab(index);
                                wdgTemplate = NULL;
                            }
                        }
                    });
                

                @GeekOwL said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

                or when close is activated are you sure you handled to not delete the closed tab and its data

                I dont know... I have no answer to this question.

                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #15

                I replace my code, i create QtreeWidgetItem only constructor, i have a following code and its work fine:

                QMap<int, ForTree> &items = globalall->tree;
                auto it = items.begin();
                auto &&topItem = new MyTree(it.value().Name, it.value().UID, it.value().Pause);
                topItem->setExpanded(true);
                ui->treeWidget->addTopLevelItem(topItem);
                this->mpTree[it.key()] = topItem;
                
                it++;
                for (; it != items.end(); it++) {
                    MyTree *m_item = new MyTree(it.value().Name, it.value().UID, it.value().Pause);
                    m_item->setExpanded(true);
                    this->mpTree[it.value().ParentUID]->addChild(m_item);
                    this->mpTree[it.key()] = m_item;
                }
                

                MyTree is a my class, sub class of QTreeWidgetItem:

                MyTree::MyTree(QVariant value, int UID, bool checked, QTreeWidgetItem *parent) : QTreeWidgetItem(parent)
                {
                    this->m_value = value;
                    this->m_parent = static_cast<MyTree*>(parent);
                    this->m_UID = UID;
                    this->m_checked = checked;
                
                    this->setData(0, Qt::EditRole, value);
                    this->setIcon(0, QIcon(":/SMSicons/folder.png"));
                    this->setCheckState(0, (checked ^ 1) ? Qt::Checked : Qt::Unchecked);
                }
                
                • But I still wonder what was the problem?

                • If my treeItems were destroyed with the tree, why i have items in my QMap<> ?

                Do what you want.

                jsulmJ 1 Reply Last reply
                0
                • Taz742T Taz742

                  I replace my code, i create QtreeWidgetItem only constructor, i have a following code and its work fine:

                  QMap<int, ForTree> &items = globalall->tree;
                  auto it = items.begin();
                  auto &&topItem = new MyTree(it.value().Name, it.value().UID, it.value().Pause);
                  topItem->setExpanded(true);
                  ui->treeWidget->addTopLevelItem(topItem);
                  this->mpTree[it.key()] = topItem;
                  
                  it++;
                  for (; it != items.end(); it++) {
                      MyTree *m_item = new MyTree(it.value().Name, it.value().UID, it.value().Pause);
                      m_item->setExpanded(true);
                      this->mpTree[it.value().ParentUID]->addChild(m_item);
                      this->mpTree[it.key()] = m_item;
                  }
                  

                  MyTree is a my class, sub class of QTreeWidgetItem:

                  MyTree::MyTree(QVariant value, int UID, bool checked, QTreeWidgetItem *parent) : QTreeWidgetItem(parent)
                  {
                      this->m_value = value;
                      this->m_parent = static_cast<MyTree*>(parent);
                      this->m_UID = UID;
                      this->m_checked = checked;
                  
                      this->setData(0, Qt::EditRole, value);
                      this->setIcon(0, QIcon(":/SMSicons/folder.png"));
                      this->setCheckState(0, (checked ^ 1) ? Qt::Checked : Qt::Unchecked);
                  }
                  
                  • But I still wonder what was the problem?

                  • If my treeItems were destroyed with the tree, why i have items in my QMap<> ?

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #16

                  @Taz742 said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

                  MyTree *m_item = new MyTree(it.value().Name, it.value().UID, it.value().Pause);

                  You don't provide a parent and I'm not sure the QTreeWidget takes the ownership of the children if you add them. If not then it would explain why they are still there.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Taz742T 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Taz742 said in QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once:

                    MyTree *m_item = new MyTree(it.value().Name, it.value().UID, it.value().Pause);

                    You don't provide a parent and I'm not sure the QTreeWidget takes the ownership of the children if you add them. If not then it would explain why they are still there.

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by
                    #17

                    @jsulm
                    Please leave it for a while.
                    Think about the problem only, because if i use QTreeWidgetItem and not MyTree i have the same problem..

                    Do what you want.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #18

                      Hi
                      I was thinking:

                      If the TreeWidget didnt not take ownership of the items, then any normal use case would leak.
                      So if the treewidget is deleted, it will delete its items too.

                      If you have a QMap with pointers to the items.
                      You map will still have the items * pointer but now they point to deleted QtreeWidgetItem (child)
                      if used once/given to a TreeWidget and this TreeWidget gets deleted.

                      Taz742T 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        Hi
                        I was thinking:

                        If the TreeWidget didnt not take ownership of the items, then any normal use case would leak.
                        So if the treewidget is deleted, it will delete its items too.

                        If you have a QMap with pointers to the items.
                        You map will still have the items * pointer but now they point to deleted QtreeWidgetItem (child)
                        if used once/given to a TreeWidget and this TreeWidget gets deleted.

                        Taz742T Offline
                        Taz742T Offline
                        Taz742
                        wrote on last edited by
                        #19

                        @mrjj
                        Yes supposedly.

                        Do what you want.

                        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