PDA

View Full Version : std::vector erase only Deletes the last entry/element



eyad
4th March 2016, 15:54
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.


//Counter.h
//copy constructor of the Counter class
Counter(const Counter& c)
:
ClickTimes(c.ClickTimes), //unsigned int
name(c.name), //Qstring
firstTime(c.firstTime), //bool
CreationDate(c.CreationDate), //Const QDate
lastDate(c.lastDate), //QDate
pumped(c.pumped) //bool
{}

// operator =() overload of Counter Class (i think the problem is here)

Counter operator=(const Counter& rhs)
{
Counter lhs(rhs);
return lhs;
}

//MainWindow.cpp

//delete Function version 1
void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
{
model->removeRow(row);

QString itsName = model->item(row,0)->text(); //name of the row
for(auto i = pCounters.begin();i != pCounters.end();i++)
{
if(i->getName() == itsName)
{
pCounters.erase(i);
saved = false;
return; //program crashes if its the last entry otherwise it deletes the last entry
}
}
}

//Delete Function Version 2
void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
{
model->removeRow(row);
if(pCounters.size() > row)
{
pCounters.erase(pCounters.begin()+ row);
saved = false;
//doesn't crash but always deletes the last entry no matter where the iterator is
//all these trials(down) yield the same result
//pCounters.erase(pCounters.end()- row);
//pCounters.erase(pCounters.begin());
//pCounters.erase(pCounters.end());
}
}

anda_skoa
4th March 2016, 19:32
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,
_

eyad
4th March 2016, 19:59
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

anda_skoa
4th March 2016, 21:02
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?



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

eyad
4th March 2016, 21:15
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- ...

anda_skoa
4th March 2016, 21:23
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,
_