Search code examples
pythonnodespyqtgraph

How to change shape of pyqtgraph nodes


I am currently working with pyqtgraph and want to change the shape of custom nodes without creating a whole new NodeCustom file. My current understanding is that the paint function in the NodeGraphicsItem class of pyqtgraph.Node manages the appearance of nodes.

I am trying to display an ellipse node. I want to override the paint function of pyqtgraph and use a custom drawEllipse method.

class EllipseNodeItem(Node):
    nodeName = "EllipseNode"

    def __init__(self, name):
        self.view = None
        # Initialize node with only a single input terminal
        n = Node.__init__(self, name, terminals={'data': {'io': 'in'}})
        #NodeGraphicsItem.__init__(self, n)

    # drawing node in shape of ellipse, fitting into rectangle 200x100
    def paint(self, p, *args):  # pyqt function
        self.bounds = QtCore.QRectF(0, 0, 200, 100)

        p.setPen(self.pen)
        if self.isSelected(): #or self in sm.selectedNodes:
            p.setPen(self.selectPen)
            p.setBrush(self.selectBrush)
        else:
            p.setPen(self.pen)
            if self.hovered:
                p.setBrush(self.hoverBrush)
            else:
                p.setBrush(self.brush)

        p.drawEllipse(self.bounds)

    def setView(self, view):  # setView must be called by the program
        self.view = view

    def process(self, data, display=True):
        if display and self.view is not None:
            # the 'data' argument is the value given to the 'data' terminal
            if data is None:
                self.view.setImage(np.zeros((1, 1)))  # give a blank array to clear the view
            else:
                self.view.setImage(data)

It does treat the EllipseNode as a separate selectable node. Obviously, paint is not used by the imported Node and NodeGraphicsItem class but that is my goal.


Solution

  • I think you need to add

    def boundingRect(self):       
        return  QtCore.QRectF(0, 0, 200, 100)
    

    If it is not answer for you, I will delete my answer...