Adding Items to Reimplemented QGraphicsScene

  • I have reimplemented QGraphicsScene to a class called MGraphicsScene as I would like to disable moving any items in case the QGraphicsScene is used for a certain purpose that I have made available through variable Mode. I have the following code for this:

    void MGraphicsScene::addItem(QGraphicsItem *Item) {
        if (Mode == INSPECTOR)
            Item->setFlag(QGraphicsItem::ItemIsMovable, false);

    On top of this, I have reimplemented MGraphicsScene to a class called StructuralScene. I am trying to add items to such StructuralScene by using the following code:

    void StructuralScene::onAddedPort(PortDefinition *Connector) {
        PortDiagram *New = new PortDiagram(Connector);

    PortDiagram is a subclass of QGraphicsObject.

    For some reason however, the PortDiagram items are not added at all to the scene. I have checked this by inspecting the result of QGraphicsScene::items() after performing StructuralScene::onAddedPort, but this list is indeed empty. Any ideas what is wrong here, i.e., why the items are not added to the scene?

  • The problem stays if I remove the MGraphicsScene::addItem method completely. So, in which direction should I be looking for potential errors?

  • I found the problem: I forgot to set the scene for my view! How stupid...

  • Qt Champions 2017

    Well on the bright side, you found it yourself. :)

  • Yes :) But I only found it because I was trying to access the scene in a context menu elsewhere ;)

  • Qt Champions 2017


    Hehe, well all productive programmers will have some sort of whoops from time to time. :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.