[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.


class Marriage(QGraphicsItem, util.Debug):

    def __init__(self, a, b):
        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

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

    def shape(self):
        return self.path
    def update(self, *args):
        if None in self.people:
        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.x(), y_off) # now retracing to see if it automatically closes the path...
        path.lineTo(a.x(), y_off) # …didn’t work though
        self.path = path
        super().update(*args) # after updating the path

    def paint(self, painter, option, widget):

    def itemChange(self, change, value):
        if change == QGraphicsItem.ItemSelectedChange:
            if value:
                self.pen = util.SELECTED_PEN
                self.pen = util.PEN
        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