Question regarding overriding QGraphicsScene



  • Hi there, I want to provide my own implementation deriving from QGraphicsScene for the purposes of providing multiple layers of depth for background, item and foreground objects and rendering within these categories in my own ordering. I appear to be missing something quite obvious (and probably rather embarrassing) in my initial stub implementation and was hoping perhaps someone could point me in the right direction. I've overrided the three drawing functions but the breakpoints in DrawBackground() and DrawItems() are not being hit. In the rest of the code I've set a background image and added a few QPixMap derived items and these are being displayed correctly. I'm calling repaint() on the view every 1/60s to trigger redraws.

    The code for the class is as follows:

    @class DisplayScene : public QGraphicsScene

    {

    Q_OBJECT
    

    public:

    explicit DisplayScene(QObject *parent = 0);
    

    protected:

    // from QGraphicsScene
    
    void drawItems(QPainter *painter, int numItems,
    
                   QGraphicsItem *items[],
    
                   const QStyleOptionGraphicsItem options[],
    
                   QWidget *widget = 0);
    
    void drawBackground(QPainter *painter, const QRectF &rect);
    
    void drawForeground(QPainter *painter, const QRectF &rect);
    

    };@

    And:

    @void DisplayScene::drawItems(QPainter *painter, int numItems,
    QGraphicsItem *items[],
    const QStyleOptionGraphicsItem options[],
    QWidget *widget)
    {
    QGraphicsScene::drawItems(painter, numItems, items, options, widget);
    }

    void DisplayScene::drawBackground(QPainter *painter, const QRectF &rect)
    {
    QGraphicsScene::drawBackground(painter, rect);
    }

    void DisplayScene::drawForeground(QPainter *painter, const QRectF &rect)
    {
    QGraphicsScene::drawForeground(painter, rect);
    }@

    Thanks for any help in pointing out what I'm missing.



  • If you set a background brush, then the QGV framework calls the function QGraphicsView::drawBackground() instead of the QGraphicsScene::drawBackground(). Similarly for the foreground stuff.

    So either:

    • Dervice a custom class from QGraphicsView and override drawBackground() and drawForeground() there or
    • Don't set a custom background/foreground brush.

    Does that solve it for you?



  • Apologies for the late reply, that's completely solved my problem with the background image thank you!

    It appears the reason for drawItems() not being called is because I missed the bit about requiring the Indirect flag to be set since Qt 4.6. I'll perform all my rendering for items in the foreground processing then as I had no intention of using the scene's default implementation.

    Thank you for the help.


Log in to reply
 

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