更改QGraphicsItem的颜色

时间:2017-10-08 19:07:18

标签: python pyqt pyqt5 qgraphicsitem

我正在开发一个简单的软件,它有一个GUI。 (我使用的是PyQt) 我有2个radiobuttons。如果选择了第一个,则通过单击Graphicsscene,将将GraphicsItem添加到场景中。 我想要一个按钮,按下它会改变这些点的颜色。颜色并不重要。例如,它可能是红色的。我怎么能这样做?谢谢!

为了放置小部件,我使用了Qt Designer,然后创建了一个名为SimpleWindow的子类。

这是代码: 分类:

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)

对话框窗口:

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_()

这是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"))

1 个答案:

答案 0 :(得分:0)

首先要创建一个改变颜色的方法,在这种情况下我们称之为@Override public void paintControl(PaintEvent event) { Canvas canvasProgress = (Canvas)event.widget; Rectangle clientArea = canvasProgress.getClientArea(); // Client area .. set colors gc.fillRectangle(clientArea.x, clientArea.y, clientArea.x + clientArea.width - 1, clientArea.y + clientArea.height - 1); gc.drawRectangle(clientArea.x, clientArea.y, clientArea.x + clientArea.width - 1, clientArea.y + clientArea.height - 1); }

setBrush()

要做的第二件事是创建按钮并将其定位在某个位置。然后QPushButton点击信号连接到某个插槽。然后我们通过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) 方法获取场景中的项目,并使用items()更改颜色:

setBrush()