[Interest] QGraphicsView selection not honoring QGraphicsItem::shape?

Patrick Stinson patrickkidd at gmail.com
Sun Jun 25 19:10:34 CEST 2017


Hi there!

I have a simple QGraphicsItem which draws the left, bottom, and right borders of it’s bounding rect. I am painting the item by creating a QGraphicsPath from top-left => bottom-left => bottom-right =>? top-right.

I have reimplemented shape() to return this path, but the view is still using the item’s bounding rect to select. I have tested this by dragging my selection marquee down through the top border of the item. I tried re-tracing the QPainterPath back to the beginning in case it was automatically closing it from top-right to top-left, but it didn’t help.

Is there something else that needs to happen to get the view to honor shape()?

See code and screenshot below.

Thanks!
-Patrick

class Marriage(QGraphicsItem, util.Debug):

    def __init__(self, a, b):
        super().__init__()
        global last_id
        self.id = last_id = last_id + 1        
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.path = QPainterPath()
        self.pen = util.PEN
        self.people = [a, b]
        self.children = []
        self.hover = False # highlight
        self.update()

    def boundingRect(self):
        return self.shape().boundingRect()

    def shape(self):
        return self.path
    
    def update(self, *args):
        if None in self.people:
            return
        a = self.mapToScene(self.people[0].bottomCenterScene())
        b = self.mapToScene(self.people[1].bottomCenterScene())
        y_off = max(a.y(), b.y()) + 50
        path = QPainterPath(a)
        path.lineTo(a.x(), y_off)
        path.lineTo(b.x(), y_off)
        path.lineTo(b)
        path.lineTo(b.x(), y_off) # now retracing to see if it automatically closes the path...
        path.lineTo(a.x(), y_off) # …didn’t work though
        path.lineTo(a)
        self.path = path
        super().update(*args) # after updating the path

    def paint(self, painter, option, widget):
        painter.save()
        painter.setPen(self.pen)
        painter.drawPath(self.path)
        painter.restore()

    def itemChange(self, change, value):
        if change == QGraphicsItem.ItemSelectedChange:
            if value:
                self.pen = util.SELECTED_PEN
            else:
                self.pen = util.PEN
            self.update()
        return super().itemChange(change, value)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170625/2c5c62f3/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: IMG_0005 2.jpg
Type: image/jpeg
Size: 164826 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170625/2c5c62f3/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1403 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170625/2c5c62f3/attachment.bin>


More information about the Interest mailing list