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......
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......
About two for loops and a couple of QGraphicsLineItems will do the trick:
http://doc.qt.nokia.com/4.6/qgraphicslineitem.html
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.....
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)
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.....
Qt Code:
{ scene->setSceneRect(0, 0, 250, 250);//rectuangular scene built 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() { } { 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
what should i write in the calling function ......
Last edited by mind_freak; 24th June 2010 at 19:27.
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.
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..
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......
Last edited by mind_freak; 24th June 2010 at 21:17.
i have done some kind of changes in my code....
On executing this code shows me the following error"virtual void QGraphicsView:drawBackground() is protected"......Qt Code:
{ scene->setSceneRect(0, 0, 250, 250);//rectuangular scene built paint->setPen(Qt::blue); view->drawBackground(paint,r1); view->show(); lay->addWidget(view); setLayout(lay); showMaximized(); } MainWindow::~MainWindow() { } { 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
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.
Bookmarks