Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter line defects when panning and zooming in QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

QPainter line defects when panning and zooming in QGraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 477 Views
  • 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.
  • johnratiusJ Offline
    johnratiusJ Offline
    johnratius
    wrote on last edited by
    #1

    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
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by gde23
      #2

      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()));
      
      johnratiusJ 1 Reply Last reply
      2
      • gde23G 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()));
        
        johnratiusJ Offline
        johnratiusJ Offline
        johnratius
        wrote on last edited by johnratius
        #3

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

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved