Python QGraphicscene中Qrubberband的渲染错误
我的q q显示不好,但我认为想要的坐标是正确的:Python QGraphicscene中Qrubberband的渲染错误,python,pyqt,pyqt5,qgraphicsview,qgraphicsscene,Python,Pyqt,Pyqt5,Qgraphicsview,Qgraphicsscene,我的q q显示不好,但我认为想要的坐标是正确的: class Viewer(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.graphicsView = QtWidgets.QGraphicsView() self.hbox = QtWidgets.QVBoxLayout() self.scene = Scene(self)
class Viewer(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.graphicsView = QtWidgets.QGraphicsView()
self.hbox = QtWidgets.QVBoxLayout()
self.scene = Scene(self)
self.splitter = QtWidgets.QSplitter()
self.splitter.addWidget(self.graphicsView)
self.widget.setLayout(self.hbox)
self.setCentralWidget(self.widget)
我在场景中加载一个pixmap:
def open_picture(self):
self.scene.setSceneRect(0, 0, self.pixmap.width(), self.pixmap.height())
self.scene.addPixmap(self.pixmap)
self.graphicsView.setScene(self.scene)
self.graphicsView.show()
我的场景是从QGraphicscene继承的,主要是在场景中处理一个qrubberband
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent)
def mousePressEvent(self, event):
self.originQPoint = event.scenePos()
self.originQPoint = self.originQPoint.toPoint()
self.currentQRubberBand = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle)
def mouseMoveEvent(self, event):
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, event.scenePos().toPoint()).normalized())
self.currentQRubberBand.show()
def mouseReleaseEvent(self, event):
print(self.items)
self.currentQRubberBand.hide()
self.currentQRect = self.currentQRubberBand.geometry()
print(self.currentQRect)
我的问题是矩形显示在我的笔记本电脑的屏幕上,但坐标正常(场景坐标)
如何在不更改self.currentQRect值的情况下在场景中正确绘制橡皮筋?根据:
QPointF qgraphicsceneumouseevent::scenePos()常量
返回场景坐标中的鼠标光标位置
从上面我们可以得出结论,我们得到的点是相对于场景的,而不是相对于屏幕的,所以这不是我们想要的
使用的方法是:
QPoint QGraphicsCenemouseEvent::screenPos()常量
返回鼠标光标在屏幕坐标中的位置
通过上述操作,我们获得以下代码:
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent)
def mousePressEvent(self, event):
self.originQPoint = event.screenPos()
self.currentQRubberBand = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle)
self.originCropPoint = event.scenePos()
def mouseMoveEvent(self, event):
self.currentQRubberBand.setGeometry(QtCore.QRect(self.originQPoint, event.screenPos()))
self.currentQRubberBand.show()
def mouseReleaseEvent(self, event):
self.currentQRubberBand.hide()
currentQRect = self.currentQRubberBand.geometry()
self.currentQRect = QtCore.QRect(self.originCropPoint.toPoint(), event.scenePos().toPoint())
print(self.currentQRect)
不,我在整个屏幕上都有一个qrubberband。scenePos是我想要的,我做了一个作物移植,它是好的。pb表示渲染没有跟随鼠标指针(?)似乎,ok(我重新实现了我的信号等,以裁剪表中的选择…)。最后一个pb(:p),qrubberband是0%透明的,而之前它是半透明的(我在rubberband矩形下看不到任何东西)。。。我想我可以用一种专门的方法修复它,但它很奇怪。无论如何,非常感谢屏幕和场景位置