Search code examples
c++qtqgraphicsviewqgraphicsscene

Ignore drawForeground() from the scene on the 2nd graphicsview in qt


I have my own QGraphicsScene and two QGraphicView’s. In the QGraphicsScene I use the

drawForeground(QPainter *painter, const QRectF &rect)

function to draw a grid. Now I want the grid only to be visible in the first, but not in the second view…is this possible?


Solution

  • The QGraphicsView is a window into a world (the QGraphicsScene). What you're asking would be like saying that it's raining when I look outside my windows, but can I have it only rain when I look through one of them?!

    However, you can change the curtains(!), so override the function of the QGraphicsView, rather than the QGraphicsScene. I suggest using this: -

    QGraphicsView::drawForeground(QPainter *, const QRectF &);
    

    Apply this just to the view you want to change. With two views, you'll need a flag to set which of the views you want to apply this to. For example: -

     void MyGraphicsView::drawForeground(QPainter* painter, const QRectF& rect)
     {
         QGrahicsView::drawForeground(painter, rect);
         if(m_bDrawGrid)
             DrawGrid(); 
     }