向QGraphicsItem [PySide]添加渐变渐变

时间:2017-08-26 18:33:14

标签: python pyqt pyside gradient linear-gradients

如何将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) 等。示例代码:

import sublime
import sublime_plugin


class SelectWordInFunctionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        prev_sel = [sel for sel in view.sel()]
        function_region = next((region for region in view.find_by_selector('meta.function') if region.contains(view.sel()[0])), None)
        if function_region:
            #word = view.expand_by_class(view.sel()[0].begin(), sublime.CLASS_WORD_START + sublime.CLASS_WORD_END)
            view.window().run_command('find_all_under')
            sel = [sel for sel in view.sel() if function_region.contains(sel)]
            view.sel().clear()
            view.sel().add_all(sel if any(sel) else prev_sel)
        else:
            view.window().status_message('Not inside a function')

1 个答案:

答案 0 :(得分:0)

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

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