Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QList of QScopedPointers
QtWS25 Last Chance

[Solved]QList of QScopedPointers

Scheduled Pinned Locked Moved General and Desktop
qscopedpointersmart pointerqlist
8 Posts 3 Posters 4.6k Views
  • 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    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;
    };
    

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Joel-Bodenmann said:

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

      Joel BodenmannJ 1 Reply Last reply
      0
      • mrjjM mrjj

        @Joel-Bodenmann said:

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

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by
        #3

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

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        A 1 Reply Last reply
        0
        • Joel BodenmannJ Joel Bodenmann

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

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @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 BodenmannJ 1 Reply Last reply
          0
          • A Asperamanca

            @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 BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on last edited by
            #5

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

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            mrjjM A 2 Replies Last reply
            0
            • Joel BodenmannJ Joel Bodenmann

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • Joel BodenmannJ Joel Bodenmann

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

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @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 BodenmannJ 1 Reply Last reply
                0
                • A Asperamanca

                  @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 BodenmannJ Offline
                  Joel BodenmannJ Offline
                  Joel Bodenmann
                  wrote on last edited by
                  #8

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

                  Industrial process automation software: https://simulton.com
                  Embedded Graphics & GUI library: https://ugfx.io

                  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 Extensions
                  • Unsolved