Question

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?

Was it helpful?

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(); 
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top