Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once
Qt 6.12 LTS is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 4 Posters 4.9k Views 1 Watching
  • 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

    Documantation says:

    QTreeWidget::~QTreeWidget()
    
    Destroys the tree widget and all its items.
    

    I think, when treeWidget is deleted, my items in my QMap are destroyed...

    But

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

    its printed My items...
    What happend?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #3

    @Taz742 Did you delete the tree widget before printing the items?

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

    Taz742T 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Taz742 Did you delete the tree widget before printing the items?

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

      @jsulm never

      Do what you want.

      jsulmJ 1 Reply Last reply
      0
      • Taz742T Taz742

        @jsulm never

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #5

        @Taz742 Then it's fine, right? Or do I misunderstand something? If ~QTreeWidget() isn't called then it does not delete anything.

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

        Taz742T 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Taz742 Then it's fine, right? Or do I misunderstand something? If ~QTreeWidget() isn't called then it does not delete anything.

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

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

          Then it's fine, right?

          Yes.

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

          If ~QTreeWidget() isn't called then it does not delete anything.

          I also dont know what happend..

          Do what you want.

          jsulmJ 1 Reply Last reply
          0
          • Taz742T Taz742

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

            Then it's fine, right?

            Yes.

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

            If ~QTreeWidget() isn't called then it does not delete anything.

            I also dont know what happend..

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #7

            @Taz742 "its printed My items...
            What happend" - to be honest I don't understand what you mean. What happened and what were you expecting to happen?

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

            Taz742T 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Taz742 "its printed My items...
              What happend" - to be honest I don't understand what you mean. What happened and what were you expecting to happen?

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

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

              Do what you want.

              jsulmJ 1 Reply Last reply
              0
              • 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 Offline
                jsulmJ Offline
                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 Offline
                    jsulmJ Offline
                    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 Offline
                              jsulmJ Offline
                              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
                                    • Unsolved