2017-08-26 16 views
0

QLinearGradient()Qrect()に追加する方法。以下に示すように、あなたは直接QLinearGradientを渡すことができQGraphicsItemに傾斜ランプを追加する[PySide]

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) 

答えて

0

class ItemClass(QGraphicsItem): 
    def __init__(self, parent=None): 
     QGraphicsItem.__init__(self, parent) 

    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

をI等 Qt.blackとしてコード例のみソリッドカラーを追加することができ
関連する問題