QGraphicsScene::drawBackground lines thinner than coord units are not drawn
-
I use the
drawBackground
method to draw a grid of straight lines.
I have a maximum availabesceneRect
size andwheelEvent
in the view connected to a slot in the scene to dynamically change the grid scale, it looks like this:
The code that does the adjustments and calculates the lines' positions:void PlotScene::drawGrid(QPainter *painter, const QRectF &rect){ QPen pen = QPen(Qt::black); pen.setCosmetic(true); double gap = 1 / gridScale, level, extraRender; double top = rect.top(), bottom = rect.bottom(), left = rect.left(), right = rect.right(); //horizontal painter->setPen(pen); extraRender = (right - left) * EXTRA_RENDER; //iterate from top to bottom and draw lines for (level = gap * round(top / gap) - gap; level < bottom + gap; level += gap) { painter->drawLine(left - extraRender, level, right + extraRender, level); } //vertical painter->setPen(pen); extraRender = (bottom - top) * EXTRA_RENDER; //iterate from left to right and draw lines for (level = gap * round(left / gap) - gap; level <= right + gap; level += gap) { painter->drawLine(level, top - extraRender, level, bottom + extraRender); } }
The loops just calculate the grid's intersection with the visible rectangle and draw the lines.
The probem is that when I zoom greatly into the scene (in terms of single coordinates), the lines are not drawn at all:
So, when line coordinates are small double values (even 0.9 counts), the lines are not seen at all (even if I specify greater pen width).
That is, I can draw a line (-5,-5,5,5), but can't draw a line (-0.9, -0.9, 0.9, 0.9). On zooming out it works perfectly (until I totally zoom out of the scene).
What is the cause of this behavior and how can I fix it to be able to zoom more into the scene with a visible grid?I tried to use
setCosmetic
andsetWidthF
methods ofQPen
, didn't help