鼠標事件,我想用Python語言編寫一個簡單的程序與PyQt的。PyQt的:在的QGraphicsView
我有一個QGraphicsScene,我想做到以下幾點: 有2個選項使用兩個單選按鈕:
- 對於生成點。這樣,如果有人點擊該場景,橢圓將出現。
- 用於選擇點。這樣,如果有人點擊某個點,所選點將被返回。
我還挺新在PyQt的以及在GUI編程。我的主要問題是我不太瞭解Qt中的鼠標事件是如何工作的。 如果有人如此善良耐心地向我解釋鼠標事件的基礎知識,併爲上述問題提供了一些解決方案,我將非常感激。
我附上圖片太,以可視化的問題。
我試圖做的問題。爲了放置我使用Qt Designer的小部件,然後創建了一個名爲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_()
這是由設計器生成的類:
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"))
謝謝。
*選定的點將返回*的含義是什麼? – eyllanesc
顯示其座標就足夠了。 –
我建議您嘗試解決它並告訴我們您嘗試過的問題。關於這個問題,網上有很多信息,就在這裏。 – eyllanesc