Results 1 to 5 of 5

Thread: Weird behavior in QList.erase

  1. #1
    Join Date
    May 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Weird behavior in QList.erase

    Hello All!
    I've created my first GUI Application, using QT C++ in Windows. Unfortunately, it throws runtime error. Can anyone help me with this code:
    Qt Code:
    1. void Manager::vectorsDifference(QVector<Data*> *vectorFirst, QVector<Data*> *vectorSecond, QVector<Data*> *vectorOutput)
    2. {
    3. QVector<Data*>::iterator itFirst;
    4. QVector<Data*>::iterator itSet;
    5. QVector<Data*>::iterator itDel;
    6. QVector<Data*> vDeleted;
    7. int iVec;
    8.  
    9. for(itFirst = vectorFirst->begin(); itFirst != vectorFirst->end(); itFirst++)
    10. {
    11. vectorOutput->push_back(*itFirst);
    12. }
    13. for(itSet = vectorOutput->begin(); itSet != vectorOutput->end(); itSet++)
    14. {
    15. for(iVec = 0; iVec != vectorSecond->size() - 1; iVec++)
    16. {
    17. if ((*itSet)->x == vectorSecond->at(iVec)->x &&
    18. (*itSet)->y == vectorSecond->at(iVec)->y )
    19. {
    20. vDeleted.push_back(*itSet);
    21. vectorOutput->erase(itSet);
    22. }
    23. }
    24.  
    25. for(itDel = vDeleted.begin(); itDel != vDeleted.end(); itDel++)
    26. {
    27. if( (*itSet)->x == (*itDel)->x &&
    28. (*itSet)->y == (*itDel)->y)
    29. {
    30. vectorOutput->erase(itSet);
    31. }
    32. }
    33. }
    34. }
    35.  
    36. class Data
    37. {
    38. public:
    39. qString x,y;
    40. }
    41.  
    42. pseudocode:
    43. QVector First { {a,a}, {a,b}, {b,a} }
    44. QVector Second { {a,a}, {a,c}, {a,b}, {a,a} }
    45. What I want:
    46. QVector output { {b,a} }
    To copy to clipboard, switch view to plain text mode 
    Description:
    Vectors(First and Second) stores data of class Data, which contains x and y info. In one vector, there might be a few similar files with the same x and y. I want to create vector of elements, which are only in one vector, but not in the other. That's why I've created another vector (vDeleted) to store data that was deleted, but it might occur again.
    The runtime exception is thrown, while data is being erased.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Weird behavior in QList.erase

    The STL style iterators for Qt containers may become invalidated when you change the contents of the container. If you want iterator stability when doing deletes on container items, look at the Java style iterators.

  3. #3
    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: Weird behavior in QList.erase

    erase() returns the iterator pointing to the next element.

    Qt Code:
    1. for (it = container.begin(); it != container.end(); /* no ++ here */) {
    2. if (condition) {
    3. it = container.erase(it);
    4. }else {
    5. // only increment if no delete happened
    6. ++it;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Weird behavior in QList.erase

    Does the following accomplish what you want?

    Qt Code:
    1. vectorOutput = vectorFirst;
    2.  
    3. QVectorIterator<Data*> itOutput(vectorOutput);
    4. QVectorIterator<Data*> itSecond(vectorSecond);
    5.  
    6. while (itOutput.hasNext())
    7. {
    8. Data *d1 = itOutput.next();
    9. vectorSecond.toFront();
    10. while (itSecond.hasNext())
    11. {
    12. Data *d2 = itSecond.next();
    13. if (d1->x == d2->x && d1->y == d2->y)
    14. {
    15. vectorDeleted.push_back(d1);
    16. vectorOutput.removeOne(d1);
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Weird behavior in QList.erase

    erase() returns the iterator pointing to the next element.
    But the trick to this is you have to continuously evaluate container.end() as part of the for() loop termination condition. You can't assign it to a variable prior to entering the for() loop, because the value it holds will become invalid as soon as the container content changes (either through insertion or erasure). In other words, this will not work:

    Qt Code:
    1. QVector<Data *>::iterator it;
    2. QVector<Data *>::iterator eIt = container.end();
    3. for (it = container.begin(); it != eIt; /* no ++ here */) {
    4. if (condition) {
    5. it = container.erase(it);
    6. }else {
    7. // only increment if no delete happened
    8. ++it;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Weird setStyleSheet() behavior in Qt 5.3
    By Carlsberg in forum Qt Programming
    Replies: 0
    Last Post: 10th June 2014, 16:00
  2. QWebview weird behavior on some pages
    By ZaQ in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2011, 03:24
  3. QWebView weird behavior !!
    By gontham in forum Newbie
    Replies: 1
    Last Post: 21st October 2010, 14:00
  4. Weird removeRow behavior
    By metalinspired in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2009, 12:42
  5. Weird QMenu behavior on Mac
    By munna in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2009, 15:18

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
  •  
Qt is a trademark of The Qt Company.