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.
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.
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:
painter->setWorldMatrixEnabled(true); qreal left = int(rect.left()) - (int(rect.left()) % m_gridSize); qreal top = int(rect.top()) - (int(rect.top()) % m_gridSize); QVarLengthArray<QLineF, 100> linesX; for (qreal x = left; x < rect.right(); x += m_gridSize) QVarLengthArray<QLineF, 100> linesY; for (qreal y = top; y < rect.bottom(); y += m_gridSize) if (m_grid == true && linesY.size()<50) { painter->setPen(m_gridColour); painter->drawLines(linesX.data(), linesX.size()); painter->drawLines(linesY.data(), linesY.size()); }To copy to clipboard, switch view to plain text mode
What is m_gridSize supposed to be? Is it the size of my scene rectangle?
nevermind, m_gridsize is like how many lines I want right?
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:
{ int gridInterval = 100; //interval to draw grid lines at painter->setWorldMatrixEnabled(true); qreal left = int(rect.left()) - (int(rect.left()) % gridInterval ); qreal top = int(rect.top()) - (int(rect.top()) % gridInterval ); QVarLengthArray<QLineF, 100> linesX; for (qreal x = left; x < rect.right(); x += gridInterval ) QVarLengthArray<QLineF, 100> linesY; for (qreal y = top; y < rect.bottom(); y += gridInterval ) painter->drawLines(linesX.data(), linesX.size()); painter->drawLines(linesY.data(), linesY.size()); }To copy to clipboard, switch view to plain text mode
Hope it helps
Last edited by robertson1; 19th January 2010 at 22:11.
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.
Bookmarks