Results 1 to 6 of 6

Thread: Trying to draw a grid on a QGraphicsScene/View

  1. #1
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trying to draw a grid on a QGraphicsScene/View

    I have a QGraphicsScene that is similar to a map, and I am trying to draw a grid on it. Is there an easier way to do this than to draw a bunch of perpendicular lines? I would rather not add a bunch of QGraphicsLineItems to the scene manually.

  2. #2
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to draw a grid on a QGraphicsScene/View

    You can reimplememt QGraphicsView::drawForeround and then use painter->drawLines. There was another thread on here somewhere that discussed fast ways of doing this, but I cant find it.

    Qt Code:
    1. painter->setWorldMatrixEnabled(true);
    2.  
    3. qreal left = int(rect.left()) - (int(rect.left()) % m_gridSize);
    4. qreal top = int(rect.top()) - (int(rect.top()) % m_gridSize);
    5.  
    6. QVarLengthArray<QLineF, 100> linesX;
    7. for (qreal x = left; x < rect.right(); x += m_gridSize)
    8. linesX.append(QLineF(x, rect.top(), x, rect.bottom()));
    9.  
    10. QVarLengthArray<QLineF, 100> linesY;
    11. for (qreal y = top; y < rect.bottom(); y += m_gridSize)
    12. linesY.append(QLineF(rect.left(), y, rect.right(), y));
    13. if (m_grid == true && linesY.size()<50)
    14. {
    15. painter->setPen(m_gridColour);
    16. painter->drawLines(linesX.data(), linesX.size());
    17. painter->drawLines(linesY.data(), linesY.size());
    18. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to draw a grid on a QGraphicsScene/View

    What is m_gridSize supposed to be? Is it the size of my scene rectangle?

  4. #4
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to draw a grid on a QGraphicsScene/View

    nevermind, m_gridsize is like how many lines I want right?

  5. #5
    Join Date
    Nov 2007
    Posts
    26
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Trying to draw a grid on a QGraphicsScene/View

    Hi sorry, that was a bit vauge. This was for a map screen which could be panned, zoomed and rotated, the grid lines were drawn at specified intervals (m_gridSize) in scene coordinates, for example if m_gridSize was 10 you would get lines at 0, 10, 20 etc units on the X and Y axis in the scene regardless of any zooming.

    Note also that in this example my scene was scaled (1,-1) so that I my Y axis was flipped (Y now increasing upwards)

    'rect' is the view rect, as 'painter->setWorldMatrixEnabled(true), then the drawing is done in scene coordinates

    Here's the example is simplified a bit:

    Qt Code:
    1. void NavScreen::drawForeground(QPainter* painter, const QRectF& rect)
    2. {
    3. int gridInterval = 100; //interval to draw grid lines at
    4. painter->setWorldMatrixEnabled(true);
    5.  
    6. qreal left = int(rect.left()) - (int(rect.left()) % gridInterval );
    7. qreal top = int(rect.top()) - (int(rect.top()) % gridInterval );
    8.  
    9. QVarLengthArray<QLineF, 100> linesX;
    10. for (qreal x = left; x < rect.right(); x += gridInterval )
    11. linesX.append(QLineF(x, rect.top(), x, rect.bottom()));
    12.  
    13. QVarLengthArray<QLineF, 100> linesY;
    14. for (qreal y = top; y < rect.bottom(); y += gridInterval )
    15. linesY.append(QLineF(rect.left(), y, rect.right(), y));
    16.  
    17. painter->drawLines(linesX.data(), linesX.size());
    18. painter->drawLines(linesY.data(), linesY.size());
    19. }
    To copy to clipboard, switch view to plain text mode 

    Hope it helps
    Last edited by robertson1; 19th January 2010 at 23:11.

  6. #6
    Join Date
    Sep 2009
    Posts
    60
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to draw a grid on a QGraphicsScene/View

    Thank you, I have got it working. Can you tell me more about your map screen?

    I am trying to do something similar to what you did. I am trying to create a map with movable icons using satellite and terrain data from USGS.

    So far I have the icons I am using at their appropriate Lat/Long points on the map. I am using QGraphicsPixmapItems as the icons. I also have the grid now for displaying the Lat/Long of the map area. I am working on getting the satellite imagery onto the QGraphicsScene.

Similar Threads

  1. Replies: 0
    Last Post: 16th August 2009, 18:46
  2. How Coulld I create a Grid Layout on a QGraphicsScene
    By c_srikanth1984 in forum Qt Programming
    Replies: 3
    Last Post: 12th January 2009, 11:18
  3. QGLWidget inside QGraphicsScene/View
    By h123 in forum Qt Programming
    Replies: 3
    Last Post: 10th January 2009, 09:46
  4. want to draw points in QGraphicsScene
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 19:14
  5. slider control and combo box in grid view
    By steg90 in forum Qt Programming
    Replies: 13
    Last Post: 21st November 2007, 11:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.