View Full Version : Removing last Painted point

28th June 2013, 10:41
Good Day

I store points in an array (points taken from position of mouse click)

In my paintEvent I draw points from array

foreach(tempMark, myPoints)
painter.drawEllipse(tempMark, 2, 2);

I created an UNDO Button that deletes the last point from array
I tested it, by writing points to file, That point is deleted

But my paintEvent still displays that point while I'm creating new points
(note all points must be displayed at all times during the run of the program)
If I click Undo, it should Remove only the last point
(which it does from the array - Button is working) But the point is still displayed on the screen


this->repaint(); //-> probably this->repaint() not working! maybe

Do I add something to the paint Event?
Any Ideas?

Many Thanks

28th June 2013, 11:36
Hi, i think that PushButton_Undo not call. Can add to you code

qDebug() << Q_FUNC_INFO;

and view console log.

28th June 2013, 14:09
The push Button is being called, Since the array is being updated
The problem is the paint function not rendering the updated array.

Ideas welcome


28th June 2013, 15:56
Add the qDebug() call to the paint event and see if it is called or not. By the way, use update() instead of repaint().

28th June 2013, 16:43
added update(); ;)

Added the above qDebug line of code, program compiles
though im not sure what i'm supposed to look for/where/...?
some guidance please

Kind Regards

29th June 2013, 17:19
You should observe whether calling your undo results in the debug statement you put in the paint event being printed.

1st July 2013, 15:56
Thanks for your help...

Please inform me,
How would I go about creating a function that can remove ANY point from screen.

Example: In running program, user creates 7+ points
User is able to click one of the previous points to remove it

Note: all points are stored in an array
How would I test The mouse position against array to delete point:confused:

Ideas Welcome;)
Kind Regards

1st July 2013, 16:28
Use mouse events to determine which of the stored point the user clicked on, remove that point from the stored list, redraw.

2nd July 2013, 09:53
My program crashes when I try removing point:confused:
//using popUpMenu to remove point

Here is my code:

void LabelClass::RemovePoint()
QList<int> lengthArray;
int min = 0;
int indexMin = 0
int tempLength = 0;

int index = 0;
while (index < myArrayCounter)
//Using Distance Formula to determine nearest point
//x1 and y1 are coordinates of mouse position at the time of click on popUpMenu
tempLength = sqrt(pow((x1 - myArray),2) + pow((y1 - myArray[index+1]), 2));

//[I]Find Smallest
min = lengthArray[index];
if (lengthArray[index] < minRe)
min = lengthArray[index];
indexMin = index;
index += 2;
//Array stored as |x|y|x|y|...
//delete x & y value & decrement counters
myCounter -= 1;
myArrayCounter -= 2;

I create this same algorithm per category of points (pointsA, pointsB,.. stored in differentArrays)
compare the smallest length of each array and delete at index of that array

Help, please
Ideas on a more efficient algorithm, most welcome!:D
Kind Regards

2nd July 2013, 11:08
I would use QPoint and QPoint::manhattanLength() to calculate the distance.

int closestPoint(const QList<QPoint> &points, const QPoint &pt) {
if(points.isEmpty()) return -1;
int minIdx = 0;
int minDistance = (points.at(0)-pt).manhattanLength();
for(int i=1;i<points.size();++i) {
int dist = (points.at(i)-pt).manhattanLength();
if(dist < minDistance) { minIdx = i; minDistance = dist; }
// cutoff: if(minDistance > 10) return -1;
return minIdx;

And then:

QList<QPoint> points;
QPoint clickedPoint;
int closest = closestPoint(points, clickedPoint);
if(closest >=0) points.removeAt(closest);

You'll probably want some cutoff on the distance not to remove points that are quite far away from the destination but are still closer than all other points.

2nd July 2013, 11:37
Yes, Will try it -> looks much more efficient

Kind Regards;)