Results 1 to 6 of 6

Thread: std::vector erase only Deletes the last entry/element

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default std::vector erase only Deletes the last entry/element

    i have a table view where each cell (in the model) is associated with a "Counter" object. all the Counter objects are saved in std::vector<Counter> pCounters .
    problem is whenever i try to delete any object from that vector, no matter what i do the std::vector erase() function always deletes the last one.
    i made two versions of the delete function just to demonstrate what things i tried which always lead to the same result. if any of this versions work (as anticipated) then problem solved.
    i will try to keep this post Simple, however if any information is needed i can add it later on.
    Qt Code:
    1. //Counter.h
    2. //copy constructor of the Counter class
    3. Counter(const Counter& c)
    4. :
    5. ClickTimes(c.ClickTimes), //unsigned int
    6. name(c.name), //Qstring
    7. firstTime(c.firstTime), //bool
    8. CreationDate(c.CreationDate), //Const QDate
    9. lastDate(c.lastDate), //QDate
    10. pumped(c.pumped) //bool
    11. {}
    12.  
    13. // operator =() overload of Counter Class (i think the problem is here)
    14.  
    15. Counter operator=(const Counter& rhs)
    16. {
    17. Counter lhs(rhs);
    18. return lhs;
    19. }
    20.  
    21. //MainWindow.cpp
    22.  
    23. //delete Function version 1
    24. void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
    25. {
    26. model->removeRow(row);
    27.  
    28. QString itsName = model->item(row,0)->text(); //name of the row
    29. for(auto i = pCounters.begin();i != pCounters.end();i++)
    30. {
    31. if(i->getName() == itsName)
    32. {
    33. pCounters.erase(i);
    34. saved = false;
    35. return; //program crashes if its the last entry otherwise it deletes the last entry
    36. }
    37. }
    38. }
    39.  
    40. //Delete Function Version 2
    41. void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
    42. {
    43. model->removeRow(row);
    44. if(pCounters.size() > row)
    45. {
    46. pCounters.erase(pCounters.begin()+ row);
    47. saved = false;
    48. //doesn't crash but always deletes the last entry no matter where the iterator is
    49. //all these trials(down) yield the same result
    50. //pCounters.erase(pCounters.end()- row);
    51. //pCounters.erase(pCounters.begin());
    52. //pCounters.erase(pCounters.end());
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by eyad; 4th March 2016 at 16:04.

  2. #2
    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: std::vector erase only Deletes the last entry/element

    Well, the obvious error in the first DeleteItem is that you get the text from the next row instead of the row you want to delete.
    The second one looks ok though.

    Do you react to changes in the model somewhere, anything that would change pCounters?

    Cheers,
    _

Similar Threads

  1. how to add an entry element on right mouse click
    By sliverTwist in forum Newbie
    Replies: 1
    Last Post: 22nd May 2013, 22:30
  2. Replies: 9
    Last Post: 23rd April 2012, 14:53
  3. qApp->quit() deletes all its children?
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2012, 00:32
  4. Copying vector of pointers to another vector
    By Momergil in forum Newbie
    Replies: 12
    Last Post: 24th September 2011, 23:09
  5. Clean project deletes makefile
    By Djony in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2006, 11:15

Tags for this Thread

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.