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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 1 Oct 2015, 15:39 last edited by Joel Bodenmann 10 Feb 2015, 06:00
    #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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Oct 2015, 15:59 last edited by
      #2

      @Joel-Bodenmann said:

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

      J 1 Reply Last reply 1 Oct 2015, 16:01
      0
      • M mrjj
        1 Oct 2015, 15:59

        @Joel-Bodenmann said:

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

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 1 Oct 2015, 16:01 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 1 Oct 2015, 16:34
        0
        • J Joel Bodenmann
          1 Oct 2015, 16:01

          @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 1 Oct 2015, 16:34 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?

          J 1 Reply Last reply 1 Oct 2015, 16:50
          0
          • A Asperamanca
            1 Oct 2015, 16:34

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

            J Offline
            J Offline
            Joel Bodenmann
            wrote on 1 Oct 2015, 16:50 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

            M A 2 Replies Last reply 1 Oct 2015, 17:11
            0
            • J Joel Bodenmann
              1 Oct 2015, 16:50

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Oct 2015, 17:11 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
              • J Joel Bodenmann
                1 Oct 2015, 16:50

                @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 2 Oct 2015, 07:42 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?

                J 1 Reply Last reply 4 Oct 2015, 15:23
                0
                • A Asperamanca
                  2 Oct 2015, 07:42

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

                  J Offline
                  J Offline
                  Joel Bodenmann
                  wrote on 4 Oct 2015, 15:23 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

                  1/8

                  1 Oct 2015, 15:39

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved