gpt4 book ai didi

python - pyqt在Qgraphicsscene中添加矩形

转载 作者:行者123 更新时间:2023-11-28 20:57:34 24 4
gpt4 key购买 nike

我有这样的场景

class Scene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent)

def mousePressEvent(self, event):
print('scene pressed')
self.wid = MyRect(event.pos(), event.pos())
self.addItem(self.wid)
self.wid.show()

我希望带有画家、鼠标事件等的类 MyRect(QtWidgets.QGraphicsRectItem) 是一个可拖动的矩形。MyRect 中的所有内容

然后我可以在场景中有很多矩形,甚至在它们之间画线等等(图表应用程序的种类),但在 MyRect、MyLine 中保留对象相关的可编辑选项,....

我想:

class MyRect(QtWidgets.QGraphicsRectItem):
def __init__(self, begin, end, parent=None):
super().__init__(parent)
self.begin = begin
self.end = end

def paintEvent(self, event):
print('painting')
qp = QtGui.QPainter(self)
qp.drawRect(QtCore.QRect(self.begin, self.end))

def mousePressEvent(self, event):
self.begin = event.pos()
self.end = event.pos()
self.update()

def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()

def mouseReleaseEvent(self, event):
self.begin = event.pos()
self.end = event.pos()
self.update()

但我不工作(绘画事件未启动,而场景中的鼠标按下事件已启动)

我没有通过网络找到我想要的东西,所以开始尝试自己做。我很确定这是一个必须知道的起点,但我找不到它

最佳答案

首先,QGraphicsItem 不是 QWidget,因此它具有那些事件并且不直接处理它们,这就是 QGraphicsView 和 QGraphicsScene 所做的。例如你说你想要一个可移动的矩形,因为 QGraphicsView 的任务很简单,没有必要覆盖:

from PyQt5 import QtCore, QtWidgets

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)

rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 100, 100))
rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
scene.addItem(rect_item)


if __name__ == '__main__':
import sys

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

如果你想改变绘制矩形的方式,你必须覆盖 paint() 方法,如下所示:

from PyQt5 import QtCore, QtGui, QtWidgets


class RectItem(QtWidgets.QGraphicsRectItem):
def paint(self, painter, option, widget=None):
super(RectItem, self).paint(painter, option, widget)
painter.save()
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.setBrush(QtCore.Qt.red)
painter.drawEllipse(option.rect)
painter.restore()

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)

rect_item = RectItem(QtCore.QRectF(0, 0, 100, 100))
rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
scene.addItem(rect_item)


if __name__ == '__main__':
import sys

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

更新:

from PyQt5 import QtCore, QtGui, QtWidgets

class GraphicsScene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(GraphicsScene, self).__init__(QtCore.QRectF(-500, -500, 1000, 1000), parent)
self._start = QtCore.QPointF()
self._current_rect_item = None

def mousePressEvent(self, event):
if self.itemAt(event.scenePos(), QtGui.QTransform()) is None:
self._current_rect_item = QtWidgets.QGraphicsRectItem()
self._current_rect_item.setBrush(QtCore.Qt.red)
self._current_rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.addItem(self._current_rect_item)
self._start = event.scenePos()
r = QtCore.QRectF(self._start, self._start)
self._current_rect_item.setRect(r)
super(GraphicsScene, self).mousePressEvent(event)

def mouseMoveEvent(self, event):
if self._current_rect_item is not None:
r = QtCore.QRectF(self._start, event.scenePos()).normalized()
self._current_rect_item.setRect(r)
super(GraphicsScene, self).mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
self._current_rect_item = None
super(GraphicsScene, self).mouseReleaseEvent(event)


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene =GraphicsScene(self)
view = QtWidgets.QGraphicsView(scene)
self.setCentralWidget(view)


if __name__ == '__main__':
import sys

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

关于python - pyqt在Qgraphicsscene中添加矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52728462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com