PDA

View Full Version : Removing a value from QList via if statement check



ebsaith
20th June 2013, 11:42
When a user clicks, the mouse position x & y value is stored in a QList<int>
User will be able to click several times

My problem is if a user clicks the same spot where he/she already clicked
that x & y value must be deleted from the array only.

This is so far what i got:


//In mouseMove function
if(ev->button()==Qt::LeftButton)
{
x1 = ev->x();
y1 = ev->y(); //x1,y1 are temp variables later in same function used to .append into array
...
}

RemoveValue function is:


void class::RemovePoint()
{
int r = 0;
do
{
//Only 1 Array stored in format x|y|x|y|x|y|x|y|x|y|...
if(Array1[r] == x1 && Array1[r+1] == y1)
{
Array1.removeAt(r);
Array1.removeAt(r);
}
r = r + 2; //Jumps to next x value
}while(r < 50); lets say 25 points, hence x & y values = 50 array elements
}


I am not worried about the display
Just need the array to be updated as it happens

Program compiles
when I test the same point
Program crashes
[Invalid parameter passing & Assert Failure :"index out of range"

Help Appreciated...Much
Thanks

nix
20th June 2013, 11:57
When you start the loop the vector has 50 entries. But after removing two of them (one point), the list have only 48 and at the last loop you got the pretty clear assert because you asking for mode data that the container have.
Use Array1.count() in your while test, it should work.

Lesiok
20th June 2013, 11:59
Because after first use lines 9 and 10 size of Array1 is 48 not 50, after next 46 and so on. What type is Array1 ?

rawfool
20th June 2013, 12:10
Any reason why you are not using QList<QPoint> or even QMap<int, int> ??

ebsaith
20th June 2013, 12:27
Tried .count()
Now program does not crash with error message
Just stops responding (I have to force close)
Any ideas

Using QList<int> because There are multiple arrays in my program each sub-categorized per type of calculation
so my actual array is in the format x|y|result|... each array with different calculation results

Thanks for the quick response :)

anda_skoa
20th June 2013, 18:31
You should not advance r if you delete at r otherwise you will skip points.

A list of structures is stil a better idea. if your "entry" is more than two integers then create a structure that holds all data per entry



struct Entry
{
int x;
int y;
int result;
};

QList<Entry> list;


Cheers,
_

ebsaith
21st June 2013, 07:39
Thanks
Kind Regards