改变 QGraphicsItem 的颜色
Changing the color of a QGraphicsItem
我正在开发一个简单的软件,它有一个 GUI。 (我正在使用 PyQt)
我有 2 个单选按钮。如果选择第一个,则通过单击 Graphicsscene,GraphicsItem 将被添加到场景中。
我想要一个按钮,按下它会改变这些点的颜色。颜色无所谓。例如,它可以是红色的。我怎么能那样做?谢谢!
为了放置小部件,我使用了 Qt Designer,然后创建了一个名为 SimpleWindow 的子class。
代码如下:
积分class:
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtWidgets import QGraphicsItem
class Point(QGraphicsItem):
def __init__(self, x, y):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.rectF = QRectF(0, 0, 4, 4)
self.x=x
self.y=y
def boundingRect(self):
return self.rectF
def paint(self, painter=None, style=None, widget=None):
painter.fillRect(self.rectF, Qt.black)
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QPen, QBrush
from PyQt5.QtWidgets import QGraphicsScene
现场:
class PointsGraphicsScene(QGraphicsScene):
def __init__(self, parent=None):
QGraphicsScene.__init__(self, parent)
self.setSceneRect(0, 0, 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":
p = point.Point(x, y)
p.setPos(x, y)
self.addItem(p)
elif self.opt == "Select":
print(x, y)
对话框window:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QButtonGroup
import window
from scene import PointsGraphicsScene
class SimpleWindow(QtWidgets.QMainWindow, window.Ui_Dialog):
def __init__(self, parent=None):
super(SimpleWindow, self).__init__(parent)
self.setupUi(self)
self.scene = PointsGraphicsScene(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())
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"))
首先是创建一个改变颜色的方法,在本例中我们将其命名为setBrush()
:
class Point(QGraphicsItem):
def __init__(self, x, y):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.rectF = QRectF(0, 0, 4, 4)
self.x=x
self.y=y
self._brush = QBrush(Qt.black)
def setBrush(self, brush):
self._brush = brush
self.update()
def boundingRect(self):
return self.rectF
def paint(self, painter=None, style=None, widget=None):
painter.fillRect(self.rectF, self._brush)
第二件事是创建按钮并将其定位在某个位置。然后 QPushButton clicked 信号连接到某个插槽。然后我们通过items()
方法获取场景的item,用setBrush()
:
改变颜色
class SimpleWindow(QtWidgets.QMainWindow, window.Ui_Dialog):
def __init__(self, parent=None):
super(SimpleWindow, self).__init__(parent)
self.setupUi(self)
self.scene = PointsGraphicsScene(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())
button = QPushButton("change color", self)
button.move(20, 140)
button.clicked.connect(self.onClicked)
def onClicked(self):
for item in self.scene.items():
item.setBrush(QColor("red"))
app = QtWidgets.QApplication(sys.argv)
form = SimpleWindow()
form.show()
app.exec_()
我正在开发一个简单的软件,它有一个 GUI。 (我正在使用 PyQt) 我有 2 个单选按钮。如果选择第一个,则通过单击 Graphicsscene,GraphicsItem 将被添加到场景中。 我想要一个按钮,按下它会改变这些点的颜色。颜色无所谓。例如,它可以是红色的。我怎么能那样做?谢谢!
为了放置小部件,我使用了 Qt Designer,然后创建了一个名为 SimpleWindow 的子class。
代码如下: 积分class:
from PyQt5.QtCore import QRectF, Qt
from PyQt5.QtWidgets import QGraphicsItem
class Point(QGraphicsItem):
def __init__(self, x, y):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.rectF = QRectF(0, 0, 4, 4)
self.x=x
self.y=y
def boundingRect(self):
return self.rectF
def paint(self, painter=None, style=None, widget=None):
painter.fillRect(self.rectF, Qt.black)
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtGui import QPen, QBrush
from PyQt5.QtWidgets import QGraphicsScene
现场:
class PointsGraphicsScene(QGraphicsScene):
def __init__(self, parent=None):
QGraphicsScene.__init__(self, parent)
self.setSceneRect(0, 0, 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":
p = point.Point(x, y)
p.setPos(x, y)
self.addItem(p)
elif self.opt == "Select":
print(x, y)
对话框window:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QButtonGroup
import window
from scene import PointsGraphicsScene
class SimpleWindow(QtWidgets.QMainWindow, window.Ui_Dialog):
def __init__(self, parent=None):
super(SimpleWindow, self).__init__(parent)
self.setupUi(self)
self.scene = PointsGraphicsScene(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())
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"))
首先是创建一个改变颜色的方法,在本例中我们将其命名为setBrush()
:
class Point(QGraphicsItem):
def __init__(self, x, y):
super(Point, self).__init__()
self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.rectF = QRectF(0, 0, 4, 4)
self.x=x
self.y=y
self._brush = QBrush(Qt.black)
def setBrush(self, brush):
self._brush = brush
self.update()
def boundingRect(self):
return self.rectF
def paint(self, painter=None, style=None, widget=None):
painter.fillRect(self.rectF, self._brush)
第二件事是创建按钮并将其定位在某个位置。然后 QPushButton clicked 信号连接到某个插槽。然后我们通过items()
方法获取场景的item,用setBrush()
:
class SimpleWindow(QtWidgets.QMainWindow, window.Ui_Dialog):
def __init__(self, parent=None):
super(SimpleWindow, self).__init__(parent)
self.setupUi(self)
self.scene = PointsGraphicsScene(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())
button = QPushButton("change color", self)
button.move(20, 140)
button.clicked.connect(self.onClicked)
def onClicked(self):
for item in self.scene.items():
item.setBrush(QColor("red"))
app = QtWidgets.QApplication(sys.argv)
form = SimpleWindow()
form.show()
app.exec_()