[PyQt] PyQt5, QGraphicsObject, Selection Indicator, and Multiple Inheritance

Erik Hvatum ice.rikh at gmail.com
Sun Mar 1 01:17:45 GMT 2015


I tried your code in this form:

 from PyQt5 import Qt
class PartFrame2dItem(Qt.QGraphicsObject, Qt.QGraphicsRectItem):
def __init__(self):        Qt.QGraphicsObject.__init__(self)
Qt.QGraphicsRectItem.__init__(self)        # A child rectangle item
    #self._rect_item = QGraphicsRectItem(self)
#self._rect_item.setZValue(-1)        self.setZValue(-1)
#self._rect_item.setBrush(QBrush(QColor(235,235,235)))
self.setBrush(Qt.QBrush(Qt.QColor(235,235,235)))
if __name__ == '__main__':    import sys    app =
Qt.QApplication(sys.argv)    gs = Qt.QGraphicsScene()    gv =
Qt.QGraphicsView(gs)    gv.show()    part_frame_2d_item =
PartFrame2dItem()    gs.addItem(part_frame_2d_item)    app.exec()


Result:
> DISPLAY=:0 dpython listdev.py
Traceback (most recent call last):
  File "listdev.py", line 20, in <module>
    part_frame_2d_item = PartFrame2dItem()
  File "listdev.py", line 12, in __init__
    self.setBrush(Qt.QBrush(Qt.QColor(235,235,235)))
TypeError: could not convert 'PartFrame2dItem' to
'QAbstractGraphicsShapeItem'

I wonder why you didn't see an error.  I see the same exception with
various PyQt5 versions on Linux, Windows, and OS X.  Are you using PyQt4?
If so, that might account for it.

Anyway, you might consider inheriting Qt.QObject rather than
Qt.QGraphicsObject if you like the multiple inheritance approach.
Alternatively, the underlying C++ code may be useful as a reference if you
decide to implement your own QGraphicsItemRect that inherits from only
Qt.QGraphicsObject:
https://github.com/qtproject/qt/blob/4.8/src/gui/graphicsview/qgraphicsitem.cpp
starting at line 8461, with selection highlight code starting line 7592.
There's not much to it :)

Cheers,
Erik

On Fri, Feb 27, 2015 at 10:30 AM, Alan Ezust <alan.ezust at gmail.com> wrote:

> I had a QGraphicsRectItem before that I needed to add signals to.
>
> I had trouble getting multiple inheritance to work between QGraphicsObject
> and QGraphicsRectItem. As soon as I tried to call self.setBrush(), the
> interpreter crashed, and I am not able to see any kind of error message
> that indicates what might be the problem.
>
>
> class PartFrame2dItem(QGraphicsObject, QGraphicsRectItem):
> def __init__(self):
>
> QGraphicsObject.__init__(self)
> QGraphicsRectItem.__init__(self)
> # A child rectangle item
> #self._rect_item = QGraphicsRectItem(self)
> #self._rect_item.setZValue(-1)
> self.setZValue(-1)
> #self._rect_item.setBrush(QBrush(QColor(235,235,235)))
> self.setBrush(QBrush(QColor(235,235,235)))
>
> Is there something wrong with my __init__?
> Why would it crash like that on the setBrush() ?
>
> So I redesigned it so that the QGraphicsObject creates a child
> QGraphicsRectItem. But now, when I select the item from the view, I don't
> see any dotted line selection indicator around the thing.
>
> Am I supposed to override and delegate some event handler or method to the
> from the QGraphicsObject to the QGraphicsRectItem in order to see that
> dotted line selection effect?
>
> Thanks for any insights you might have.
>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150228/76b6d41e/attachment.html>


More information about the PyQt mailing list