tags:

views:

27

answers:

1

I am rendering a QPixmap inside of a QThread. the code to paint is inside a function. If I declare the painter inside the drawChart function everything seems ok but if I declare the painter inside the run function the image is wrong in the sense that at the edge of a black and white area, the pixels at the interface are overlapped to give a grey. Does anyone know why this is so? Could it be because of the nature of the run function itself?

//This is ok  
void RenderThread::run()  
{  
  QImage image(resultSize, QImage::Format_RGB32);  
  drawChart(&image);  
  emit renderedImage(image, scaleFactor);  
}

drawChart(&image)  
{  
  QPainter painter(image);
  painter.doStuff()(;  
  ...  
}  

//This gives a image that seems to have artifacts  
void RenderThread::run()  
{  
  QImage image(resultSize, QImage::Format_RGB32);  
  QPainter painter(image);  
  drawChart(painter);  
  emit renderedImage(image, scaleFactor);  
}  

drawChart(&painter)
{   
  painter.doStuff();    
  ...  
}

//bad
Valid XHTML.
//good
Valid XHTML.

+2  A: 

From C++ GUI Programming with Qt 4 by Jasmin Blanchette and Mark Summerfield:

One important thing to understand is that the center of a pixel lies on “half-pixel” coordinates. For example, the top-left pixel covers the area between points (0, 0) and (1, 1), and its center is located at (0.5, 0.5). If we ask QPainter to draw a pixel at, say, (100, 100), it will approximate the result by shifting the coordinate by +0.5 in both directions, resulting in the pixel centered at (100.5, 100.5) being drawn.

This distinction may seem rather academic at first, but it has important consequences in practice. First, the shifting by +0.5 only occurs if antialiasing is disabled (the default); if antialiasing is enabled and we try to draw a pixel at (100, 100) in black, QPainter will actually color the four pixels (99.5, 99.5), (99.5, 100.5), (100.5, 99.5), and (100.5, 100.5) light gray, to give the impression of a pixel lying exactly at the meeting point of the four pixels. If this effect is undesirable, we can avoid it by specifying half-pixel coordinates, for example, (100.5, 100.5).

baysmith