PDA

View Full Version : GraphicsScene Background repainting



manojmka
18th December 2007, 07:13
Hi guys,

I want to have a grid on my graphicsScene of customized size. I have a serious problem with GraphicsScene background drawing. I have implemented the drawBackground() function in my graphicsScene class as follows:


painter->drawRect(rect);
if(gridSize>0)
{
painter->setOpacity(0.1);
for(int i=0;i<rect.width();i+=gridSize)
painter->drawLine(i,0,i,rect.height());
for(int i=0;i<rect.height();i+=gridSize)
painter->drawLine(0,i,rect.width(),i);
}

The grid is repainted every time I move, resize, rotate any item on the scene. And this repainting leaves some extra lines drawn on the scene?? It looks very ugly. Please help me as this is urgent!!

Regards,
Manoj

Gopala Krishna
18th December 2007, 08:32
If rect you have used is the parameter passed in drawBackground(), then your code is wrong!
That is because rect is the rectangle to be repainted and its size differs in each call
For example try
qDebug() << rect
in drawBackground() and watch out the values.

Have a look at this thread. It might be helpful
http://qtcentre.org/forum/f-qt-programming-2/t-drawing-grids-efficiently-in-qgraphicsscene-5609.html

manojmka
18th December 2007, 09:22
I am sorry for the confusion as I just renamed some parameters in the code and then posted. The rect I am using here is the sceneRect. Any solution?

Regards,
Manoj

manojmka
18th December 2007, 11:33
Thanks Gopala. The thread you reffered has proven a great help for me. Now I am just using the view's cacheBackground flag and it is working perfectly fine for me.
Initially I created a seperate item class to draw the Grid, but this option is a better one. :-)