如何在PYQT中使用Cross Cursor实现SNAP网格?

发布于 2025-01-28 15:35:02 字数 1848 浏览 1 评论 0原文

我想像波纹管一样绘制一个像图片一样的快照网格,我可以绘制背景点,但是我不知道如何在鼠标移动时捕捉网格点,在这里有人知道该怎么做吗?谢谢。

我的示例代码
它开始很慢,有时卡住了。

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()

想要快照网格

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文