2

I use a QPainter to draw my widget with this code:

QPen pen(Qt::black, 0.6, Qt::SolidLine);
QPainter painter(this);
painter.setPen(pen);

// vertical 
painter.drawLine(startX,0,startX,50);
painter.drawLine((startX += grid),0,startX,50);
painter.drawLine((startX += grid),0,startX,50);
painter.drawLine((startX += grid),0,startX,50);
painter.drawLine((startX += grid),0,startX,50);
painter.drawLine((startX += grid),0,startX,50);

// horizontal 
pen.setWidth(0.7);
painter.setPen(pen);
painter.drawLine(0,grid*2,70,grid*2);
painter.drawLine(0,grid*4,70,grid*4);
painter.drawLine(0,grid*6,70,grid*6);
painter.drawLine(0,grid*8,70,grid*8);

When I add this item into a QGraphicsScene, the width of the lines sometimes look different from each other, especially when I zoom in. Can anyone explain why this is happening and what can be done to fix it?

This screen shot demonstrates the problem:

enter image description here

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
nils
  • 97
  • 1
  • 8

1 Answers1

3

This is a side effect of floating point rounding and scene interpolation/rendering. At most zoom levels, there will not be a perfect one-to-one match from scene pixels to view pixels. This is especially true for fractional pen widths. You can make things look a bit smoother by turning on anti-aliasing in your QGraphicsView:

...
view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
....

There are other rendering hints that can be passed in as well.

tomvodi
  • 5,577
  • 2
  • 27
  • 38
Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • thanks a lot, it looks much better. The edge of line is not so sharp, a little blurry. my problem also happens to QGraphicsPixelItem, the quality of pics decrease quite a lot, sometimes the line just disappeared, sometimes it becomes wider, if there a solution for this? as you know you will not observe this behavior when you use some picture viewer to scale the image. – nils Mar 19 '12 at 22:37
  • 1
    If you mean `QGraphicsPixmapItem` there is a render hint for that too. I've added it to my answer above. – Arnold Spence Mar 19 '12 at 22:50