How to implement snap to grid?

I want to implement snap to grid functionality. I have drawn grid using drawBackground function and I want that when I draw anything on grid it should come at the grid points only. I have different entities like circle, line, point, arc etc. How should I do that so that my cursor automatically goes at the gridPoint when I click on the graphicsView?

Problem is that there is no getMousePosition function in qt. Can you explain me with the help of small running example

im not sure what qt is, and im not sure which class you are trying to get the mouse position from, but viewportClients can get Mouse Coordinates. if you can get ahold of a player controller, they have a localPlayer called Player, which has a viewportClient.

FVector2D MousePosition;
if (CastChecked(Player)->ViewportClient->GetMousePosition(MousePosition))
{
MousePosition.x = GridSize * FMath::RoundToFloat(MousePosition.x/GridSize);
MousePosition.y = GridSize * FMath::RoundToFloat(MousePosition.y/GridSize);
}
return MousePosition;