'How to implement snap grid with cross cursor in pyqt?

I want to draw a snap grid like the picture as bellow,I can draw the background point, but I don't know how to snap the grid point while mouse moving,Could here someone know how to do it?Thanks.

My Sample Code
it start very slow and sometime stuck.

import cv2 as cv
import numpy as np

import math

from qtpy.QtWidgets import *
from qtpy.QtCore import *
from qtpy.QtGui import *

class Point(QGraphicsItem):
    def boundingRect(self) -> QRectF:
        return QRectF(-3, -3, 6, 6)

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget) -> None:
        painter.setPen(Qt.NoPen)
        painter.setBrush(Qt.red)
        painter.drawEllipse(self.boundingRect())

class Scene(QGraphicsScene):
    def __init__(self):
        super().__init__()
        self.setItemIndexMethod(QGraphicsScene.BspTreeIndex)
        self.setBackgroundBrush(Qt.lightGray)
        self.initScene()

    def initScene(self):
        y = x =  np.arange(1000*100, step=50)
        xx, yy = np.meshgrid(x, y)
        for px, py in zip(xx.flatten(), yy.flatten()):
            point = Point()
            point.setPos(px, py)
            self.addItem(point)

    def wheelEvent(self, event: 'QGraphicsSceneWheelEvent') -> None:
        factor = math.pow(2.718, event.delta()/360)
        factorRatio = self.views()[0].transform().scale(factor, factor).mapRect(QRectF(0, 0, 1, 1)).width()
        if factorRatio < 0.3 or factorRatio > 5:
            return

        self.views()[0].scale(factor, factor)
        return super().wheelEvent(event)

app  = QApplication([])
view = QGraphicsView()
view.setRenderHints(QPainter.Antialiasing)
view.setScene(Scene())
view.resize(500, 500)
view.show()
app.exec()

Wanted the snap grid
enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source