PDA

View Full Version : Trying to draw a grid on a QGraphicsScene/View



di_zou
19th January 2010, 15:39
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.

robertson1
19th January 2010, 17:21
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.


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)
linesX.append(QLineF(x, rect.top(), x, rect.bottom()));

QVarLengthArray<QLineF, 100> linesY;
for (qreal y = top; y < rect.bottom(); y += m_gridSize)
linesY.append(QLineF(rect.left(), y, rect.right(), y));
if (m_grid == true && linesY.size()<50)
{
painter->setPen(m_gridColour);
painter->drawLines(linesX.data(), linesX.size());
painter->drawLines(linesY.data(), linesY.size());
}

di_zou
19th January 2010, 19:24
What is m_gridSize supposed to be? Is it the size of my scene rectangle?

di_zou
19th January 2010, 20:54
nevermind, m_gridsize is like how many lines I want right?

robertson1
19th January 2010, 22:06
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:


void NavScreen::drawForeground(QPainter* painter, const QRectF& rect)
{
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 )
linesX.append(QLineF(x, rect.top(), x, rect.bottom()));

QVarLengthArray<QLineF, 100> linesY;
for (qreal y = top; y < rect.bottom(); y += gridInterval )
linesY.append(QLineF(rect.left(), y, rect.right(), y));

painter->drawLines(linesX.data(), linesX.size());
painter->drawLines(linesY.data(), linesY.size());
}

Hope it helps

di_zou
21st January 2010, 15:55
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.