PDA

View Full Version : QCanvas clipping problems



ksierens
18th September 2007, 19:34
I need to be able to clip my canvas to a given rectangle. I tried sub-classing from the QCanvas widget and added my own virtual functions for drawForeground and drawBackground. I then tried to set clipping on and set a clip rect for the painter in both functions, which didn't work.
So then I tried to just add a return statement at the top of both functions, but the foreground always gets painted.
Can someone tell me what I am doing wrong, and more importantly, why is it drawing anything at all when I am just returning from the draw functions?

wysota
19th September 2007, 10:37
I think it will be easiest if you reimplement drawContents() from QCanvasView. There you can clip the painter to a desired rectangle and call the base class implementation.

ksierens
19th September 2007, 12:55
Yeah that's what I thought too. Here is what I tried, which still draw the entire canvas:

void myCanvasView::drawContents(QPainter *p, int cx, int cy, int cw, int ch)
{
QRect myRect(20, 20, 50, 50);

p->setClipRect(myRect);
p->setClipping(true);

QCanvasView::drawContents(p, myRect.x(), myRect.y(), myRect.width(), myRect.height());
}

wysota
19th September 2007, 13:22
You should certainly call the base class implementation with the values you received, so cx, cy, cw and ch in this case. And check if your implementation is called at all.

marcel
19th September 2007, 14:36
No, you should call the base class version with the values you clip the painter: 20, 20, 50 ,50.

wysota
19th September 2007, 16:04
No, you should call the base class version with the values you clip the painter: 20, 20, 50 ,50.

Hmm... Why so?