【问题标题】:Add gradient ramp to QGraphicsItem将渐变渐变添加到 QGraphicsItem
【发布时间】:2018-02-04 12:20:39
【问题描述】:

如何将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)

【问题讨论】:

    标签: python gradient pyside linear-gradients qgraphicsitem


    【解决方案1】:

    你可以直接传递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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-27
      • 2016-04-20
      • 2019-09-16
      • 2015-09-02
      • 2020-12-06
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      相关资源
      最近更新 更多