[Solved]QList of QScopedPointers
-
wrote on 1 Oct 2015, 15:39 last edited by Joel Bodenmann 10 Feb 2015, 06:00
I have a
QList<QScopedPointer<Label>>
whereLabel
is a custom class. I can successfully add new pointers to the list and use them. However, I cannot callQList::removeAll()
because this would require moving the pointer. Can somebody tell me how to resolve this issue?Code:
class Label { QString string; }; class Component { public: explicit Component() { mLabels.append(QScopedPointer<Label>(new Label)); mLabels.append(QScopedPointer<Label>(new Label)); mLabels.append(QScopedPointer<Label>(new Label)); } void foo() { for (QScopedPointer<Label>& label : mLabels) { mLabels.removeAll(label); } } private: QList<QScopedPointer<Label>> mLabels; };
-
@Joel-Bodenmann said:
It's not possible to use T * QScopedPointer::take()
to gain ownership and then delete it? -
@Joel-Bodenmann said:
It's not possible to use T * QScopedPointer::take()
to gain ownership and then delete it?wrote on 1 Oct 2015, 16:01 last edited by@mrjj I don't want to delete the pointer, I just want to remove it from the list.
-
@mrjj I don't want to delete the pointer, I just want to remove it from the list.
wrote on 1 Oct 2015, 16:34 last edited by@Joel-Bodenmann
When you remove the ScopedPointer from the list, it gets destructed and will delete the pointer it contains. With this construct, you need to save the contained object first, then delete the scoped pointer from the list.However, it may be that what you want can be achieved in a better way. What is your need in a bigger context?
-
@Joel-Bodenmann
When you remove the ScopedPointer from the list, it gets destructed and will delete the pointer it contains. With this construct, you need to save the contained object first, then delete the scoped pointer from the list.However, it may be that what you want can be achieved in a better way. What is your need in a bigger context?
wrote on 1 Oct 2015, 16:50 last edited by@Asperamanca
I have a custom QGraphicsItem that is made up of several other QGraphicsItem (QGraphicsItemGroup won't do the job in my case). I keep a list of the 'children' items as a private member in the big item class. Everything works with regular raw pointers but I recently learned about smart pointers so I thought I will give it a try. Maybe this is not a good application for them. -
@Asperamanca
I have a custom QGraphicsItem that is made up of several other QGraphicsItem (QGraphicsItemGroup won't do the job in my case). I keep a list of the 'children' items as a private member in the big item class. Everything works with regular raw pointers but I recently learned about smart pointers so I thought I will give it a try. Maybe this is not a good application for them.@Joel-Bodenmann
Well it is sort special use case as normally smart pointers are used to make sure
that dynamic allocated objects are deleted when they runs out of scope.So If you wanted the Labels to live and die with mLabels
the idea was fine but you actually
have other more dynamic needs.It should be possible to safe keep the Label
and destroy the QScopedPointer but then it only ensures that whatever remains
in list will be deleted and it might be easier just to let Component delete the
Labels in the destructor. IMO -
@Asperamanca
I have a custom QGraphicsItem that is made up of several other QGraphicsItem (QGraphicsItemGroup won't do the job in my case). I keep a list of the 'children' items as a private member in the big item class. Everything works with regular raw pointers but I recently learned about smart pointers so I thought I will give it a try. Maybe this is not a good application for them.wrote on 2 Oct 2015, 07:42 last edited by@Joel-Bodenmann said:
@Asperamanca
I have a custom QGraphicsItem that is made up of several other QGraphicsItem (QGraphicsItemGroup won't do the job in my case). I keep a list of the 'children' items as a private member in the big item class. Everything works with regular raw pointers but I recently learned about smart pointers so I thought I will give it a try. Maybe this is not a good application for them.Even though you marked it as solved: Why not just use the parent-child mechanism of QGraphicsItem in this case?
-
@Joel-Bodenmann said:
@Asperamanca
I have a custom QGraphicsItem that is made up of several other QGraphicsItem (QGraphicsItemGroup won't do the job in my case). I keep a list of the 'children' items as a private member in the big item class. Everything works with regular raw pointers but I recently learned about smart pointers so I thought I will give it a try. Maybe this is not a good application for them.Even though you marked it as solved: Why not just use the parent-child mechanism of QGraphicsItem in this case?
wrote on 4 Oct 2015, 15:23 last edited by@Asperamanca
I am actually not sure anymore. I thought that there was an issue regarding theboundingRect()
implementation but I have changed my design since then. I will give this another thought.
1/8