Results 1 to 12 of 12

Thread: best way to delete QVector<QVector3D>

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default best way to delete QVector<QVector3D>

    What is the best way to delete
    Qt Code:
    1. QVector<QVector3D> vertices
    To copy to clipboard, switch view to plain text mode 

    I already cleared it. Now for deleting it, I tried to use qDeleteAll() function, but
    it asks for begin and end iterators. I do not know how to iterate through all vectors and call qDeleteAll(). Nor do I know if that is really necessary.
    On the other hand, vertices.reserve(0) also does not delete the whole thing. (I don't think so)

    what's your idea?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: best way to delete QVector<QVector3D>

    Don't do anything. Let it go out of scope and all will be done for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: best way to delete QVector<QVector3D>

    but what if it doesn't go out of scope. I need to delete after right after drawing.

    Qt Code:
    1. qDeleteAll(vertices.begin(), vertices.end());
    To copy to clipboard, switch view to plain text mode 

    gives the following error:
    error: type 'class QVector3D' argument given to 'delete', expected pointer
    Qt Code:
    1. template <typename ForwardIterator>
    2. Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
    3. {
    4. while (begin != end) {
    5. delete *begin;
    6. ++begin;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: best way to delete QVector<QVector3D>

    Quote Originally Posted by saman_artorious View Post
    but what if it doesn't go out of scope. I need to delete after right after drawing.
    Clear the vector.

    Qt Code:
    1. vertices.clear();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: best way to delete QVector<QVector3D>

    Quote Originally Posted by wysota View Post
    Don't do anything. Let it go out of scope and all will be done for you.
    in this case it will be overloaded. not deleted i guess.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: best way to delete QVector<QVector3D>

    What will be overloaded? Overloaded with what?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: best way to delete QVector<QVector3D>

    Quote Originally Posted by wysota View Post
    What will be overloaded? Overloaded with what?
    if by letting it go out of scope you mean to push to it. when it reaches the boundary of allocation, it starts from the beginning overloading.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: best way to delete QVector<QVector3D>

    I'm sorry, I don't understand what you mean.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: best way to delete QVector<QVector3D>

    Hi,

    What wysota want to tell you is that:
    Qt Code:
    1. void myClass::myMethod()
    2. {
    3. QVector<QVector3D> vector;
    4. ... //use of the vector
    5. }
    To copy to clipboard, switch view to plain text mode 

    When the method call exits, the QVector<QVector3D> will be cleared.
    If you want to delete the vector when it still is in the scope you have to call clear:

    Qt Code:
    1. void myClass::myMethod()
    2. {
    3. QVector<QVector3D> vector;
    4. ... //use of the vector
    5. vector.clear(); //This clears the vector
    6. ... //Here you can reuse the vector with new data
    7. }
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: best way to delete QVector<QVector3D>

    A scope or block scope is a visiblity and validiity construct of C and languages with similar syntax such as C++, Java, etc.

    Names, e.g. variables, declared in a scope are usually only visible within the scope and nested scopes, not outside, i.e. not in the parent scope or sibling scopes.

    Languages such as C and C++, which allow data on the stack, destroy such data when the variable's scope ends.

    Qt Code:
    1. int main()
    2. {
    3. // inside scope of main()
    4. int a;
    5.  
    6. a = 3; // valid
    7.  
    8. {
    9. int b;
    10.  
    11. // inside a nested scope
    12. a = 4; // valid
    13. b= 4; // valid
    14. }
    15.  
    16. a = 5; // valid;
    17. b = 5; // will not compile, b only visible/valid in nested scope
    18. }
    To copy to clipboard, switch view to plain text mode 

    In C++, an object such as std::vector is destroyed (its destructor is invoked) when the scope it lives in ends.
    Qt Code:
    1. int main()
    2. {
    3. vector<int> a;
    4.  
    5. {
    6. vector<int> b;
    7.  
    8. }// b is destroyed
    9.  
    10. }// a is destroyed
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. #11
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: best way to delete QVector<QVector3D>

    You misunderstood me. vertices is a member variable of a class and not a local variable. I want to delete it after I construct it.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: best way to delete QVector<QVector3D>

    Since it is a class member you can't delete it. To delete it you'd have to delete the whole class instance. You can only clear the vector which will remove its contents.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 22nd June 2013, 10:02
  2. sort a Qvector based on another Qvector
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2012, 06:39
  3. Replies: 5
    Last Post: 2nd September 2011, 23:11
  4. QVector3D, real
    By zaphod77 in forum Newbie
    Replies: 7
    Last Post: 24th March 2011, 19:04
  5. Replies: 4
    Last Post: 19th February 2009, 11:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.