PDA

View Full Version : Strange Painter behaviour



cwomeijer
4th September 2006, 17:29
Hi Everyone,

I seem to have some odd behaviour I can't explain.
Here is my code:

void ScribbleArea::drawGrid()
{
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));

for(int y=0; y<image.height(); y+=gridSize)
for(int x=0; x<image.width(); x+=gridSize)
painter.drawPoint(x,y);
}

void ScribbleArea::drawPolygon()
{
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));

#if 0
drawGrid();
#else
for(int y=0; y<image.height(); y+=gridSize)
for(int x=0; x<image.width(); x+=gridSize)
painter.drawPoint(x,y);
#endif

painter.drawLine(myPolygon);
modified = true;
}

void ScribbleArea::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.drawImage(QPoint(0, 0), image);
}

Please note that this code copied from the scribble example!
Anyway I wanted to add my own grid drawing routine
Which seems to work just fine when called seperatly.

However if drawPolygon() calls drawGrid() the grid never shows
up. I do get to the routine but I simply don't see the grid drawn.
If I copy the routine into the drawPolygon() (see the #if 0)
then it does work.
Can someone explain this?

Regards

Otto

jpn
4th September 2006, 17:50
I suspect it has something to do with that you initialize 2 parallel painters on the image.
Does it work if you use the same painter, like this?


void ScribbleArea::drawGrid(QPainter* painter)
{
for(int y=0; y<image.height(); y+=gridSize)
for(int x=0; x<image.width(); x+=gridSize)
painter->drawPoint(x,y);
}

void ScribbleArea::drawPolygon()
{
QPainter painter(&image);
...
drawGrid(&painter);
...
}

cwomeijer
4th September 2006, 20:52
Yes, that was what I also suspected.
However after the modifications the behaviour was
identical as before.

It really looks like it doesn't like to call any other methods
while being in drawPolygon().