@necertainly said in Pixel perfect drawing with QCanvasPainter - is it possible on scaled desktop?:
Or you explicitly render in device pixel coordinates
How is this possible, though? To my knowledge there is no API in QCanvasPainter that allows me to pass device pixel coordinates.
Query the actual devicePixelRatio from the paint device instead of hardcoding 1.5
Sure, it's only hardcoded for sake of clarity. 1.5 is what devicePixelRatioF() returns as well.
Snap coordinates to device pixels after scaling, not before
QCP does the scaling internally, so it's not really possible for me to "snap coordinates to device pixels after scaling not before". I can only pass reverse-scaled coordinates to QCP to counteract its scaling, which I'm trying to do, but clearly not achieving very well. What I'd like, is ideally to be able to turn off scaling in QCP completely, or at least to know the exact scaling formula.
snap to integer pixel centers
That's not how GPU accelerated graphics works. At least not on D3D, which Qt uses on Windows. Device pixel centers are not at integer coordinates - https://learn.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-coordinates
Disable antialiasing when drawing hairlines
That works. Somewhat. But only when the canvas has an even-numbered size. When the canvas size is odd, something happens that makes the pixels shift - the farther they are from origin. I realize, that I accumulate some error in the loop. But A) the float type is still precise enough for my case, and B) the error is the same at x = 600, no matter whether the canvas width is 640 or 641 (logical) pixels. Yet, in the second case, the pixels are drawn 1px off.
Imagine that you resize the window. It produces horrible flickering. It flickers whether AA is on or off - the only difference is that with AA it's blurred. Now, why would it flicker, if I draw exactly the same lines at exactly the same (logical) pixel coordinates? This surely smells like a bug in QCanvasWidget or QCanvasPainter.
[image: 3ab2d0e0-d002-4b83-9b5f-6c7cae2dfe03.png]