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. QGraphicsScene::drawBackground lines thinner than coord units are not drawn

QGraphicsScene::drawBackground lines thinner than coord units are not drawn

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 217 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.
  • A Offline
    A Offline
    Asquator
    wrote on last edited by
    #1

    I use the drawBackground method to draw a grid of straight lines.
    I have a maximum availabe sceneRect size and wheelEvent in the view connected to a slot in the scene to dynamically change the grid scale, it looks like this:
    685fcc2c-cb6e-4e5d-8c7f-f536acce9921-grid.png
    The code that does the adjustments and calculates the lines' positions:

    void PlotScene::drawGrid(QPainter *painter, const QRectF &rect){
    
        QPen pen = QPen(Qt::black);
        pen.setCosmetic(true);
    
        double gap = 1 / gridScale, level, extraRender;
        double top = rect.top(), bottom = rect.bottom(), left = rect.left(), right = rect.right();
    
        //horizontal
        painter->setPen(pen);
        extraRender = (right - left) * EXTRA_RENDER;
    
        //iterate from top to bottom and draw lines
        for (level = gap * round(top / gap) - gap; level < bottom + gap; level += gap) {
            painter->drawLine(left - extraRender, level,
                            right + extraRender, level);
        }
    
        //vertical
        painter->setPen(pen);
        extraRender = (bottom - top) * EXTRA_RENDER;
    
        //iterate from left to right and draw lines
        for (level = gap * round(left / gap) - gap; level <= right + gap; level += gap) {
            painter->drawLine(level, top - extraRender,
                            level, bottom + extraRender);
        }
    }
    

    The loops just calculate the grid's intersection with the visible rectangle and draw the lines.

    The probem is that when I zoom greatly into the scene (in terms of single coordinates), the lines are not drawn at all:
    085be419-75da-42e6-a556-0a6d87784046-brokengrid.png
    So, when line coordinates are small double values (even 0.9 counts), the lines are not seen at all (even if I specify greater pen width).
    That is, I can draw a line (-5,-5,5,5), but can't draw a line (-0.9, -0.9, 0.9, 0.9). On zooming out it works perfectly (until I totally zoom out of the scene).
    What is the cause of this behavior and how can I fix it to be able to zoom more into the scene with a visible grid?

    I tried to use setCosmetic and setWidthF methods of QPen, didn't help

    1 Reply Last reply
    0

    • Login

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