Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPainter line defects when panning and zooming in QGraphicsView



  • I am drawing a grid using QPainter and pen strokes.

    Here is the loop for the X-axis, the same thing is used for the Y-axis.

    for (int xi = int(left); xi <= int(right); ++xi)
    	{
    		if (xi % 8 == 0)
    			painter->setPen(QPen(QColor::fromRgb(0, 0, 0, 100), 2, Qt::DashLine));
    		else if (xi % 4 == 0)
    			painter->setPen(QPen(QColor::fromRgb(0, 0, 0, 100), 1, Qt::DashLine));
    		else
    			painter->setPen(QPen(QColor::fromRgb(0, 0, 0, 100), 1, Qt::DotLine));
    
    		painter->drawLine(xi * step, bottom * step,
    								xi * step, top * step);
    	}
    

    My problem is when I pan around the scene, I begin to see small defects in the grid. Specifically, from the Qt::DotLine pen (It does happen with lines that use Qt::DashLine as well). Some of the dots look closer together, or disappear completely, or look spread out. I thought this was a problem with my program, until I resized the window, or selected another open window on my system. The defects reverted back to normal and it looked great after. I am assuming it's a problem with QPainter or QGraphicsView.

    My question is why is this happening, and if this is just a problem with QPainter? If it is a problem with QPainter and not my program, is there a workaround to keep the defects from appearing? I thought about calling a resize function of the window or something similar whenever the mouse moves or gets pressed, but I fear it will affect my performance greatly. Please comment if you have any suggestions I should try.

    (I can't show a picture because when I select the Window Snipping tool, my grid reverts back to normal)



  • It could be a problem with aliasing.
    Have you set the RennderHint to do anti aliasing
    (see here: https://doc.qt.io/archives/qt-4.8/qpainter.html#RenderHint-enum)?

    Edit: You mention that resizing it solves the problem. So have you tried to manually update the widget after panning around? It might be a timing issu there.

    You can try add following at the end of your pan function:

    QTimer::singleShot(0, your_graphics_widget, SLOT(repaint()));
    


  • It could be a problem with aliasing.
    Have you set the RennderHint to do anti aliasing
    (see here: https://doc.qt.io/archives/qt-4.8/qpainter.html#RenderHint-enum)?

    Edit: You mention that resizing it solves the problem. So have you tried to manually update the widget after panning around? It might be a timing issu there.

    You can try add following at the end of your pan function:

    QTimer::singleShot(0, your_graphics_widget, SLOT(repaint()));
    


  • @gde23 Good idea, I tried adding the last line but it didn't do anything. I still got some issues when zooming in and out and panning. When I resized the window, it cleared up again. I used the QTimer as such: QTimer::singleShot(0, this, SLOT(repaint())); and I also put it in the zoom function. I'll try the aliasing.

    Edit: Turns out, it was the aliasing. I set
    painter->setRenderHints(QPainter::Antialiasing); at the top of my drawBackground() function and it seems to work well now.


Log in to reply