问题
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:
回答1:
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.
来源:https://stackoverflow.com/questions/9777936/why-do-my-my-line-widths-looks-different-in-a-qgraphicsscene-with-the-same-qpen