Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QPainter line defects when panning and zooming in QGraphicsView

    General and Desktop
    2
    3
    197
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • johnratius
      johnratius last edited by

      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)

      1 Reply Last reply Reply Quote 0
      • gde23
        gde23 last edited by gde23

        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()));
        
        johnratius 1 Reply Last reply Reply Quote 2
        • gde23
          gde23 last edited by gde23

          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()));
          
          johnratius 1 Reply Last reply Reply Quote 2
          • johnratius
            johnratius @gde23 last edited by johnratius

            @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.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post