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. QVector remove behaviour
Forum Update on Monday, May 27th 2025

QVector remove behaviour

Scheduled Pinned Locked Moved Solved General and Desktop
qvectorremove
3 Posts 3 Posters 5.5k 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.
  • Q Offline
    Q Offline
    QT-static-prgm
    wrote on 2 Aug 2015, 09:27 last edited by A Former User 12 Sept 2016, 14:10
    #1

    Hi,

    i have a question about the remove function from the QVector.
    I have a QVector<SellInfo*> as member in a class.

    SellInfo is defined that way:

    struct SellInfo {
    		QString name;
    		QSpinBox* box;
    };
    

    So now i'd like to know how the destructor of my class/Vector will work.
    Step 1: i delete the class instance
    Step 2: the class's destructor deletes the QVector and the SpinBoxes (because they are children of the class)
    Step 3: QVector ???

    Does QVector delete all the SellInfo structs, or not?? and if the QVector does not, how can i delete them? I would use the remove, or erease function, but will these function only remove the pointer from the vector, or will they delete the objects, the pointer refers to?

    And if they delete, will the struct destructor delete the QSpinBox pointer?? if so, they'll be deleted twice and that will cause big problems.

    I hope you can help me with that, and i realy hope that the documentation will be updated with these informations

    1 Reply Last reply
    1
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 2 Aug 2015, 09:43 last edited by Chris Kawa 8 Feb 2015, 09:44
      #2

      QVector's destructor deletes its contents. The contents in this case are pointers so it deletes the pointers. It does not delete the things the pointers point to, so neither the structs nor their members get deleted.

      If you want to delete the things the pointers point to use qDeleteAll.
      This will delete the structs but will not delete the spinboxes.
      To delete spinboxes too give SellInfo a proper destructor that will do that.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 2 Aug 2015, 09:49 last edited by mrjj 8 Feb 2015, 09:50
        #3

        Hi

        When you insert pointers in Qvector, all it ever will delete is the pointer as it would be unsafe for it to delete the object pointed too as it is outside the vector.

        If you declare it as not pointer

        QVector<SellInfo>

        Then the SellInfo struct lives in the vector and will be deleted when QVec is.

        Deleting a SellInfo would never delete "box" as it will not call box destructor.
        (its a pointer)

        A good rule is that you bring into the world with new, should be removed with delete.

        But in Qt, many of the Wigets takes ownership of childs and you do not need to call delete on them.

        1 Reply Last reply
        1

        2/3

        2 Aug 2015, 09:43

        • Login

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