Nominate our 2022 Qt Champions!

Polymorphism fails with derived class of QGraphicsScene

  • Hi,

    I've derived QGraphicsScene into a class called DiagramScene like this :


    #include <QGraphicsScene>
    class QGraphicsSceneMouseEvent;
    class QMenu;
    class QPointF;
    class QGraphicsLineItem;
    class QFont;
    class QGraphicsTextItem;
    class QColor;
    class DiagramScene : public QGraphicsScene
        explicit DiagramScene(QMenu *itemMenu, QObject *parent = 0);
      // ... The rest ain't relevant for the problem IMO

    DiagramScene constructor:

    DiagramScene::DiagramScene(QMenu *itemMenu, QObject *parent)
        : QGraphicsScene(parent)
        // irrelevant code, this was just to show you 
        // i called QGraphicsScene constructor at initialization

    And then, the problem :

    // I voluntarily simplified the code to illustrate the problem
    DiagramScene myDiagramScene(....);
    QGraphicsView myGraphicView(....);
    // At compilation I get this error :
    // No matching function for call to QGraphicsView::setScene(DiagramScene*)
    //  Candidate : QGraphicsView::setScene(QGraphicsScene*)

    Please correct me if I'm wrong, which I certainly am, but I thought i would benefit from the polymorphism mechanism here.
    This code works if I cast the pointer into a QGraphicsScene pointer, but I feel like this should work without the case.

    Does someone have a clue?
    Thanks a lot for reading !

  • First thing I would check is your #includes. It sounds like the compiler at that point doesn't know the two classes are related - I often get this when a class is forward-declared, but I don't have the proper include where I want to convert types.

Log in to reply