QPainter.drawLine missing half pixels

  • Hello guys,

    I have a very weird problem, I am drawing 2 lines that are supposed to be in contact but there is a gap of roughly 1/2 pixel ...
    As you can see in the following code my line is 1 pixel and the gap is roughly half of its width ...

    pen = QtGui.QPen()

    I won't post more code as it would be way too many lines and extracting few lines wouldn't mean anything out of context. I'm confirming that the start point of the 2nd line is exactly the same value as the end point of the 1st line.


    So how can it miss half a pixel ??? Am I missing some antialiasing parameters ? but on 1 pixel I don't know how it would produce any visible changes.

    I've noticed that it happens if both lines are vertical, if i have 1 vertical and 1 horizontal it works just fine.

    Have you guys ever had a similar problem ?

    Thx ! :)

  • Do the lines start/end on exactly the same pixel, or on neighboring pixels?

  • So what cap type have you set on the pen?

    That might make the difference.

  • Thank your for your replies, I had a 1pixel offset before the 1st line and that's what caused this problem. BUT I am still having the same kind of problem.

    If i set my pen cap style to Flat, I have a very very small space like 1/10 (or less) of a pixel but I can see that space as the background is dark ...

    So I've tried Square cap but now I've the opposite problem, the lines are riding on top of each other and I can see them too, it looks better when I zoom in but worse when I zoom out ...

    Definetly looks better than before with Flat cap but not 100% there yet.

  • Do you use antialiasing? (see QPainter::renderHints)?
    Can you try adding 0.5 to all vertical line coordinates?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.