I’m working on a node editor using PyQt4 and have got the basics of QGraphicsView and QGraphicsItem sorted out but am having trouble with transparency.
My code is below, it looks similar to samples I’ve found, but when I move the nodes around, trails are left behind as per the image linked.
Have I missed some flag that needs to be set or some kind of update call somewhere?
def paint(self, painter, option, widget=None):
painter.setPen(Node.shadow_color)
painter.setBrush(Node.shadow_color)
painter.setOpacity(0.3)
shadow_rect = self.rect()
shadow_rect.translate(Node.shadow_offset, Node.shadow_offset)
painter.drawRoundedRect(shadow_rect, Node.corner_radius, Node.corner_radius)
if self.check_state(Node.SELECTED):
painter.setPen(Node.outline_selected_colour)
painter.setBrush(Node.background_selected_colour)
else:
painter.setPen(Node.outline_colour)
painter.setBrush(Node.background_colour)
if self.check_state(Node.HOVER):
painter.setPen(Node.outline_hover_colour)
painter.setBrush(Node.background_hover_colour)
painter.setOpacity(1.0)
painter.drawRoundedRect(self.rect(), Node.corner_radius, Node.corner_radius)
What’s happening is the QGraphicsView is updating only the bounding area defined by your node’s QRect area.
As you drag your node from the previous position, your shadow was actually drawn outside of your item’s bounding rect (as defined by the QGraphicsRectItem.rect()), so the view doesn’t redraw that space – which is why you’re seeing the trail.
You can try setting the QGraphicsView.viewportUpdateMode to something like FullViewportUpdate and see if it fixes your problem (tho it will come with a performance hit), but I’d recommend treating your node’s rect as your bounding drawing area – and offset the actual size of your node and its shadow to draw within that space vs. drawing the border and shadow outside that space.
I have a node widget if you want to see (or use) it as part of the open-source ProjexUI library of PyQt extensions. It’s found at:
http://dev.projexsoftware.com/projects/projexui
The class is projexui.widgets.xnodewidget, and I use it for a lot of different things, but the Orbiter app for instance uses it.