Results 1 to 6 of 6

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

  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,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

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

    well....something weird is happening.
    in the first DeleteItem when i changed the QString itsName = model->item(row,0)->text(); to QString itsName = model->item(row-1,0)->text();
    it selected the item over the one which i selects i.e "if i select row 2 it deletes the counter of row 1" however the model changes perfectly fine, but still it takes the name ( text() ) of the previous row???? -thats messed up , i can't determine if the problem is in the model or in the pcounter operations.

    and about the second question... i don't know about that... you mean like the DeleteItem is called with something else?
    anyways, i will attach the source code to ease things more

    the delete window
    Attached Files Attached Files
    Last edited by eyad; 4th March 2016 at 19:58. Reason: added attachment

  4. #4
    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

    Quote Originally Posted by eyad View Post
    well....something weird is happening.
    in the first DeleteItem when i changed the QString itsName = model->item(row,0)->text(); to QString itsName = model->item(row-1,0)->text();
    That is obviously also wrong, isn't it?

    If you have three rows
    A
    B
    C

    and you removeRow(1), how do you expect to get "B" by either accessing rows 0 or 1?

    Quote Originally Posted by eyad View Post
    and about the second question... i don't know about that... you mean like the DeleteItem is called with something else?
    No, I mean maybe you have connected slots to signals of the model and then you modify pCounter in one of these slots.

    In any case, this looks a lot like you want a model that works on the counter vector instead of having a model and the vector side by side.
    That way they cannot get out of sync.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

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

    Quote Originally Posted by anda_skoa View Post
    That is obviously also wrong, isn't it?

    If you have three rows
    A
    B
    C

    and you removeRow(1), how do you expect to get "B" by either accessing rows 0 or 1?

    Cheers,
    _
    well it doesn't get row B at all , it just either get A or C unless that 0 is a range... row(1,0) unless 1,0 is a range -which i don't think it is- ...

  6. #6
    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

    Quote Originally Posted by eyad View Post
    well it doesn't get row B at all , it just either get A or C
    Exactly, because B has been removed.
    So you won't be able to get it no matter which index you use.

    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.