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. QGraphicsView - background grid - shifts/moves when pan/zooom...
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView - background grid - shifts/moves when pan/zooom...

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicssceneqgraphicsview
3 Posts 2 Posters 1.9k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by Dariusz
    #1

    Hey

    I've reimplemented scene drawBackground to generate a custom looking grid like :

        qreal left = int(rect.left()) - (int(rect.left()) % mGridSize);
        qreal top = int(rect.top()) - (int(rect.top()) % mGridSize);
    
        QVarLengthArray<QLineF, 100> lines;
    
        for (qreal x = left; x < rect.right(); x += mGridSize)
            lines.append(QLineF(x, rect.top(), x, rect.bottom()));
        for (qreal y = top; y < rect.bottom(); y += mGridSize)
            lines.append(QLineF(rect.left(), y, rect.right(), y));
    
        QVarLengthArray<QLineF, 100> thickLines;
    
        for (qreal x = left; x < rect.right(); x += mGridSize * 5)
            thickLines.append(QLineF(x, rect.top(), x, rect.bottom()));
        for (qreal y = top; y < rect.bottom(); y += mGridSize * 5)
            thickLines.append(QLineF(rect.left(), y, rect.right(), y));
    
        QPen myPen(Qt::NoPen);
        painter->setBrush(QBrush(QColor(55, 55, 55, 255)));
        painter->setPen(myPen);
        painter->drawRect(rect);
    
        QPen penHLines(QColor(75, 75, 75), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin);
        painter->setPen(penHLines);
        painter->drawLines(lines.data(), lines.size());
    
        painter->setPen(QPen(QColor(100, 100, 100), 2, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
        painter->drawLines(thickLines.data(), thickLines.size());
    
    
        painter->setPen(Qt::blue);
    
        QVector<QPointF> points;
        for (qreal x = left; x < rect.right(); x += mGridSize) {
            for (qreal y = top; y < rect.bottom(); y += mGridSize) {
                points.append(QPointF(x, y));
            }
        }
        painter->drawPoints(points.data(), points.size());
    

    But when I place item close to gird lines/snap it and then move/zoom the view. The grid miss-aligns and changes its position...

    How can I correct it?

    TIA

    EDIT I've move the function to view to capture pan/zoom events to see if I can somehow control the offset for the "thick lines" drawing...

    Here is the gif showing issue :
    (0_1563500205284_2019-07-18_17-25-18.gif

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dariusz
      wrote on last edited by Dariusz
      #2

      I've updated the question with gif/some updates... Still fighting with it o.o

      I think by using int vertOffset = horizontalScrollBar()->sliderPosition();
      I can somehow calculate the "jump" value offset and offset position of line by that. Not sure how yet tho...

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WSTaiYi
        wrote on last edited by WSTaiYi
        #3

        Scale_5.gif
        可以参考这个网站进行进行背景图的绘制,缩放的原理 等https://www.desmos.com/calculator?lang=zh-CN
        同时参考这这个链接,进行背景绘制https://stackoverflow.com/questions/24728477/infinite-grid-in-qt

        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