问题
I would like to write a simple program in Python with PyQt.
I have a QGraphicsScene and I would like to do the following: There are 2 options using two RadioButtons:
- For generating points. This way if someone clicks on the scene an ellipse will appear.
- For selecting points. This way if someone clicks on a point the selected point will be returned.
I'm kinda new at PyQt and also at GUI programming. My main problem is that I don't really understand how mouse events work in Qt. If someone was so kind and patient to explain me the basics of mouse events and gave me some tipps for the problem explained above, I would be very grateful.
I attach a picture too, to visualize the problem.
I was trying to do the problem. For placing the widgets I used the Qt Designer, and then created a subclass called SimpleWindow.
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_()
This is the class generated by the Designer:
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"))
Thank you.
回答1:
In a QGraphicsView
a QGraphicsScene
is added, each one manages a system of different coordinates. the QGraphicsView
is similar to a camera and a QGraphicsScene
is similar to the world, when one adds an item to the scene it must be in its coordinate system.
As you want to add items when you click, it is better to overwrite the mousePressEvent
method of QGraphicsScene
, and get the position in the coordinates of the scene for which the scenePos()
method is used.
Another thing to do is to initialize the attribute setSceneRect()
which is the space that QGraphicsView
can see.
A recommendation if several buttons are used use a QButtonGroup
that maps the buttons making easy the easy handling of the signals.
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())
来源:https://stackoverflow.com/questions/46382141/pyqt-mouse-events-in-qgraphicsview