PDA

View Full Version : How to make a Grid on QGraphicscene



mind_freak
23rd June 2010, 20:59
hi
i want to make a kinda of a grid view with all rows an columns.........on QGraphicscene i have seen the previous thread on this ....but was not clear......

tbscope
23rd June 2010, 21:24
About two for loops and a couple of QGraphicsLineItems will do the trick:
http://doc.qt.nokia.com/4.6/qgraphicslineitem.html

mind_freak
23rd June 2010, 22:00
ok i ll try for it ..but let me ask a questionf for further .....is it possible to add a label to the specific one square of grid view.....

tbscope
23rd June 2010, 22:14
Off course.

You could, for example, create a custom QGraphicsItem that displays the grid. A GridItem for example.
In this grid item, you can have a function like setLabel(const QString &text, int column, int row);
In the implementation, you create a new QGraphicsTextItem for example

The possibilities are only limited by your imagination.

mind_freak
24th June 2010, 18:48
hey hi
thanks for the previous reply......actually i have use the properties of QGraphicsView that is drawForeground()......for drawing gridlines....the code is given below..
see the comments in red.....

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
lay=new QVBoxLayout();
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 250, 250);//rectuangular scene built
view=new QGraphicsView(scene);
view->drawForeground(); //What should be the calling parameter for this function ......i knw that its QPainter and QRectF but its not working perhaps i am writing wrong.....
view->show();
lay->addWidget(view);
setLayout(lay);
showMaximized();
}

MainWindow::~MainWindow()
{

}

void MainWindow::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());
}


what should i write in the calling function ......

aamer4yu
24th June 2010, 19:35
drawForeground is called automatically. You get the painter and you draw using it.

Also to draw the grid lines, it will be better to use drawBackground, since you will want other items on top of it.

When you draw lines, you will know the pos in scene, so you can align your widget to that scene position.

mind_freak
24th June 2010, 19:48
thanx....but friend i have one silly question that is .......what should i write in the parameter list in the given calling function;

view->drawBackground(//i knw that its QPainter and QRectF but its not working perhaps i am writing wrong.....);

i have declared this function in header .....also i have implemented this function but while call i am confused what to write in parameter list..

mind_freak
24th June 2010, 19:54
ok now i got it ....u want to say the drawBackground() is also automatically call......but who helps to call this function automatically......???i mean is how this function is automatically called......

mind_freak
25th June 2010, 09:33
i have done some kind of changes in my code....

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
lay=new QVBoxLayout();
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 250, 250);//rectuangular scene built
view=new QGraphicsView(scene);
paint=new QPainter(this);
paint->setPen(Qt::blue);
QRectF r1(100, 200, 11, 16);
view->drawBackground(paint,r1);
view->show();
lay->addWidget(view);
setLayout(lay);
showMaximized();
}

MainWindow::~MainWindow()
{

}

void MainWindow::drawBackground(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());
}

On executing this code shows me the following error"virtual void QGraphicsView:drawBackground() is protected"......
i am nt getting that where's the error......

tbscope
25th June 2010, 09:38
On executing this code shows me the following error"virtual void QGraphicsView:drawBackground() is protected"......
i am nt getting that where's the error......

The error means this:
The function drawBackground() is a protected memeber of QGraphicsView. It's not publicly available.
This in turn means that you can not directly use this function. You can only access it via a subclass or friend class.

If you really want to reimplement drawBackground, create a new subclass based on QGraphicsView.
If you think that's a lot of hassle to do, you can use the Z value to place your grid underneath all the other objects.