QTreeWidget from my QMap<int, QTreeWidgetItem*> populate only once
-
@Taz742 Then it's fine, right? Or do I misunderstand something? If ~QTreeWidget() isn't called then it does not delete anything.
@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..
-
@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..
-
@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? -
@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.@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?
-
@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?
-
@jsulm
First click on my button
Result is good, i have 2 treeitem:
now close this tab and click again my button and insert tab, result is:
-
@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 tryedqDebug() << ui->treeWidget->topLevelItemsCount();
after end loop, and result is 0.
-
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; }
@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.
-
@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.
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<> ?
-
-
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<> ?
@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.
-
-
@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.
-
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. -
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.