PyQt:QGraphicsView 中的鼠标事件

PyQt: Mouse events in QGraphicsView

我想用 PyQt 在 Python 中编写一个简单的程序。

我有一个 QGraphicsScene,我想执行以下操作: 使用两个 RadioButtons 有 2 个选项:

  1. 用于产生积分。这样如果有人点击场景就会出现一个椭圆。
  2. 用于选择点。这样,如果有人点击一个点,将返回选定的点。

我在 PyQt 和 GUI 编程方面有点新。我的主要问题是我并不真正理解鼠标事件在 Qt 中是如何工作的。 如果有人如此耐心地向我解释鼠标事件的基础知识并为我提供上述问题的一些提示,我将不胜感激。

我也附上一张图片,让问题形象化。

我正在尝试做这道题。为了放置小部件,我使用了 Qt Designer,然后创建了一个名为 SimpleWindow.

的子class
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QPen, QBrush
from PyQt5.QtWidgets import QGraphicsScene
import points

class SimpleWindow(QtWidgets.QMainWindow, points.Ui_Dialog):

    def __init__(self, parent=None):
        super(SimpleWindow, self).__init__(parent)
        self.setupUi(self)

        self.graphicsView.scene = QGraphicsScene()
        self.graphicsView.setScene(self.graphicsView.scene)
        self.graphicsView.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)

        self.graphicsView.mousePressEvent = self.pixelSelect


    def pixelSelect(self, event):
        pen = QPen(QtCore.Qt.black)
        brush = QBrush(QtCore.Qt.black)

        x = event.x()
        y = event.y()

        if self.radioButton.isChecked():
            print(x, y)
            self.graphicsView.scene.addEllipse(x, y, 4, 4, pen, brush)

       if self.radioButton_2.isChecked():
            print(x, y)


app = QtWidgets.QApplication(sys.argv)
form = SimpleWindow()
form.show()
app.exec_()

这是设计师生成的class:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(538, 269)
        self.graphicsView = QtWidgets.QGraphicsView(Dialog)
        self.graphicsView.setGeometry(QtCore.QRect(130, 10, 371, 221))
        self.graphicsView.setObjectName("graphicsView")
        self.radioButton = QtWidgets.QRadioButton(Dialog)
        self.radioButton.setGeometry(QtCore.QRect(20, 30, 82, 31))
        self.radioButton.setObjectName("radioButton")
        self.radioButton_2 = QtWidgets.QRadioButton(Dialog)
        self.radioButton_2.setGeometry(QtCore.QRect(20, 80, 82, 17))
        self.radioButton_2.setObjectName("radioButton_2")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.radioButton.setText(_translate("Dialog", "Generate"))
        self.radioButton_2.setText(_translate("Dialog", "Select"))

谢谢。

在一个QGraphicsView中增加一个QGraphicsScene,每一个管理一个不同坐标的系统。 QGraphicsView 类似于相机,QGraphicsScene 类似于世界,当向场景添加项目时,它必须在其坐标系中。

因为要在点击的时候添加物品,所以最好重写QGraphicsScenemousePressEvent方法,获取[=17]的场景坐标中的位置=] 方法被使用。

另一件事是初始化属性setSceneRect(),也就是QGraphicsView可以看到的space。

如果使用多个按钮,建议使用映射按钮的 QButtonGroup,以便轻松处理信号。

class GraphicsScene(QGraphicsScene):
    def __init__(self, parent=None):
        QGraphicsScene.__init__(self, parent)
        self.setSceneRect(-100, -100, 200, 200)
        self.opt = ""

    def setOption(self, opt):
        self.opt = opt

    def mousePressEvent(self, event):
        pen = QPen(QtCore.Qt.black)
        brush = QBrush(QtCore.Qt.black)
        x = event.scenePos().x()
        y = event.scenePos().y()
        if self.opt == "Generate":
            self.addEllipse(x, y, 4, 4, pen, brush)
        elif self.opt == "Select":
            print(x, y)


class SimpleWindow(QtWidgets.QMainWindow, points.Ui_Dialog):
    def __init__(self, parent=None):
        super(SimpleWindow, self).__init__(parent)
        self.setupUi(self)

        self.scene = GraphicsScene(self)
        self.graphicsView.setScene(self.scene)
        self.graphicsView.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)

        group = QButtonGroup(self)
        group.addButton(self.radioButton)
        group.addButton(self.radioButton_2)

        group.buttonClicked.connect(lambda btn: self.scene.setOption(btn.text()))
        self.radioButton.setChecked(True)
        self.scene.setOption(self.radioButton.text())