如何在PYQT中使用Cross Cursor实现SNAP网格?
我想像波纹管一样绘制一个像图片一样的快照网格,我可以绘制背景点,但是我不知道如何在鼠标移动时捕捉网格点,在这里有人知道该怎么做吗?谢谢。
我的示例代码
它开始很慢,有时卡住了。
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论