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

Alan Ezust alan.ezust at gmail.com
Sun Mar 1 03:39:53 GMT 2015

On Sat, Feb 28, 2015 at 5:17 PM, Erik Hvatum <ice.rikh at gmail.com> wrote:

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

I am not sure why the main program causes my interpreter to crash but I can
make a test case similar to yours and now I see the same problem.
Interesting, this TypeError makes me think that I didn't initialize the
object properly.

> Anyway, you might consider inheriting Qt.QObject rather than
> Qt.QGraphicsObject if you like the multiple inheritance approach.

Good idea! I tried that and I still get a similar TypeError, only now it is
earlier in the self.setZValue() call, and the message says "could not
convert PartFrame2dItem to QGraphicsItem."
So at least now I can see what the exception is - that is helpful. But I
don't understand why I am getting this TypeError.

> 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 :)

Thanks! Yes, I was thinking of delving into the source and seeing what i
can cannabilize from here. But if I can get multiple inheritance from
QObject and QGraphicsRectItem working, that would make my life a lot easier.

> 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
> _______________________________________________
> 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/928b58fc/attachment-0001.html>

More information about the PyQt mailing list