[SOLVED] How to create make not visible range so when user clicks near point, the point will be chosen


I have a set of vector points, with each click on my window I add pair to the vector. I want to add an invisible radius on my point which will help me to detect if a point was clicked. The point is basically 1 pixel in size so the user is not able to click on it directly. How can I achieve this? Do I need to use any math formulas for this?

std::vector<QPoint> pointSet;

void MyWindow::mousePressEvent(QMouseEvent *event)
    if(event->button() == Qt::LeftButton)
        x0 = event->x();
        y0 = event->y();
        QPoint data;
  //I want to be able to do it like below
  if(event->button() == Qt::RightButton)
        //if(pointClicked) { cout << "point with x,y clicked"; }


If I understand your question correctly, you just need to run through your vector and check if any of the saved points are within a range of the clicked point. So something like this should work:

    if (event->button() == Qt::RightButton)
        int radius = <something>
        QRectF range(event->x() - radius, event->y() - radius, radius * 2, radius * 2);

        for (auto &p in pointSet) 
            if (range.contains(p)) 
                cout << "point with" << p.x() << "," << p.y() << "clicked"; 

Answered By – JarMan

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *