Why do my my line widths looks different in a QGraphicsScene with the same QPen width?

 ̄綄美尐妖づ 提交于 2019-12-11 13:12:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!