Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
I just wanted to set the parent of an QTreeWidget AFTER it is created. But for some reason the function is not available. Any ideas why?? I can set the parent in the constructor and i can read that parent with .parent() but i cannot set it.
use addChild method of QTreeWidgetItem.
Let's say I add a few Childs and subchilds and later I delete the tree widget. Will that automatically delete all entries in the tree widget??
Matt86 last edited by
In general, any time you delete a UI object any of the children will be deleted too.
@QT-static-prgm Why do you think there is no setParent() method? QTreeWidget has setParent() method (inherited from QObject). QObject has setParent method, that means ALL classes derived from QObject have it.
but the qtreewidgetItem has no such function. My compiler gives an error
QTreeWidgetItem does not have setParent. QTreeWidget has. So you should use addChild only. QTreeWidgetItem is not inherited from QObject. QTreeWidget is inherited from QObject. So QTreeWidget has setParent.
All right thank you :D
No i have an other problem.
When i use .clear() to remove everything and then refill the tree it seams that the actual items are still alive. Is there a better way to delete them??
(i noticed that when i refreshed 3 times the connect signal is triggered 3 times)
Really? Are you using normal SIGNAL/SLots or Lambda functions?
Lambdas don't get automatically removed.
Anyway you can try something like this, to delete the children.
for(auto a : treewidget->children() delete a;
i use lambda.
there's your problem then. Either use 'normal' functions, or manually disconnect the connection.
How to do that is taken from here
New way using QMetaObject::Connection QMetaObject::Connection m_connection; //… m_connection = QObject::connect(…); //… QObject::disconnect(m_connection); Works in all cases, including lambda functions or functors.
i changed to singal/slot instead of lambda and had the same problem. Only after i disconnected manual using disconnect(ptr, signal, ptr, slot) it worked.
So it's fine for me that it works, but does that mean that there is still garbage in the memory or is it now correctly deleted??