[Solved]QList of QScopedPointers



  • I have a QList<QScopedPointer<Label>> where Label is a custom class. I can successfully add new pointers to the list and use them. However, I cannot call QList::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;
    };
    

  • Qt Champions 2016

    @Joel-Bodenmann said:

    It's not possible to use T * QScopedPointer::take()
    to gain ownership and then delete it?



  • @mrjj I don't want to delete the pointer, I just want to remove it from the list.



  • @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?



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


  • Qt Champions 2016

    @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



  • @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?



  • @Asperamanca
    I am actually not sure anymore. I thought that there was an issue regarding the boundingRect() implementation but I have changed my design since then. I will give this another thought.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.