向QGraphicsItem添加渐变渐变

2024-10-01 19:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将QLinearGradient()添加到Qrect()。我只能添加纯色作为Qt.black等。示例代码:

class ItemClass(QGraphicsItem):
    def __init__()
        super(ItemClass, self).__init__()
           # 

    def boundingRect(self):
        return QRectF(x, y, w, h)

    def paint(self, painter, opt, w):
        rec = self.boundingRect()
        painter.fillRect(rec, #Here I need gradient ramp)

Tags: 代码self示例initdefqtclassblack
1条回答
网友
1楼 · 发布于 2024-10-01 19:24:22

您可以直接传递QLinearGradient,如下所示:

class ItemClass(QGraphicsItem):
    def boundingRect(self):
        x, y, w, h = -25, -25, 50, 50
        return QRectF(x, y, w, h)

    def paint(self, painter, opt, w):
        rect = self.boundingRect()
        lgrad = QLinearGradient(rect.topLeft(), rect.bottomLeft())
        lgrad.setColorAt(0.0, Qt.red);
        lgrad.setColorAt(1.0, Qt.yellow)
        painter.fillRect(rect, lgrad)

enter image description here

相关问题 更多 >

    热门问题