Results 1 to 10 of 10

Thread: How to make a Grid on QGraphicscene

  1. #1
    Join Date
    Mar 2009
    Location
    india->gujarat->bhavnagar
    Posts
    36
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Question How to make a Grid on QGraphicscene

    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......

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to make a Grid on QGraphicscene

    About two for loops and a couple of QGraphicsLineItems will do the trick:
    http://doc.qt.nokia.com/4.6/qgraphicslineitem.html

  3. #3
    Join Date
    Mar 2009
    Location
    india->gujarat->bhavnagar
    Posts
    36
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to make a Grid on QGraphicscene

    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.....

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to make a Grid on QGraphicscene

    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.

  5. The following user says thank you to tbscope for this useful post:

    mind_freak (24th June 2010)

  6. #5
    Join Date
    Mar 2009
    Location
    india->gujarat->bhavnagar
    Posts
    36
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to make a Grid on QGraphicscene

    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:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent)
    3. {
    4. lay=new QVBoxLayout();
    5. scene = new QGraphicsScene();
    6. scene->setSceneRect(0, 0, 250, 250);//rectuangular scene built
    7. view=new QGraphicsView(scene);
    8. 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.....
    9. view->show();
    10. lay->addWidget(view);
    11. setLayout(lay);
    12. showMaximized();
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17.  
    18. }
    19.  
    20. void MainWindow::drawForeground(QPainter* painter, const QRectF& rect)
    21. {
    22. int gridInterval = 100; //interval to draw grid lines at
    23. painter->setWorldMatrixEnabled(true);
    24.  
    25. qreal left = int(rect.left()) - (int(rect.left()) % gridInterval );
    26. qreal top = int(rect.top()) - (int(rect.top()) % gridInterval );
    27.  
    28. QVarLengthArray<QLineF, 100> linesX;
    29. for (qreal x = left; x < rect.right(); x += gridInterval )
    30. linesX.append(QLineF(x, rect.top(), x, rect.bottom()));
    31.  
    32. QVarLengthArray<QLineF, 100> linesY;
    33. for (qreal y = top; y < rect.bottom(); y += gridInterval )
    34. linesY.append(QLineF(rect.left(), y, rect.right(), y));
    35.  
    36. painter->drawLines(linesX.data(), linesX.size());
    37. painter->drawLines(linesY.data(), linesY.size());
    38. }
    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.

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make a Grid on QGraphicscene

    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.

  8. #7
    Join Date
    Mar 2009
    Location
    india->gujarat->bhavnagar
    Posts
    36
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to make a Grid on QGraphicscene

    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..

  9. #8
    Join Date
    Mar 2009
    Location
    india->gujarat->bhavnagar
    Posts
    36
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: How to make a Grid on QGraphicscene

    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.

  10. #9
    Join Date
    Mar 2009
    Location
    india->gujarat->bhavnagar
    Posts
    36
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Question Re: How to make a Grid on QGraphicscene

    i have done some kind of changes in my code....
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent)
    3. {
    4. lay=new QVBoxLayout();
    5. scene = new QGraphicsScene();
    6. scene->setSceneRect(0, 0, 250, 250);//rectuangular scene built
    7. view=new QGraphicsView(scene);
    8. paint=new QPainter(this);
    9. paint->setPen(Qt::blue);
    10. QRectF r1(100, 200, 11, 16);
    11. view->drawBackground(paint,r1);
    12. view->show();
    13. lay->addWidget(view);
    14. setLayout(lay);
    15. showMaximized();
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20.  
    21. }
    22.  
    23. void MainWindow::drawBackground(QPainter* painter, const QRectF& rect)
    24. {
    25. int gridInterval = 100; //interval to draw grid lines at
    26. painter->setWorldMatrixEnabled(true);
    27.  
    28. qreal left = int(rect.left()) - (int(rect.left()) % gridInterval );
    29. qreal top = int(rect.top()) - (int(rect.top()) % gridInterval );
    30.  
    31. QVarLengthArray<QLineF, 100> linesX;
    32. for (qreal x = left; x < rect.right(); x += gridInterval )
    33. linesX.append(QLineF(x, rect.top(), x, rect.bottom()));
    34.  
    35. QVarLengthArray<QLineF, 100> linesY;
    36. for (qreal y = top; y < rect.bottom(); y += gridInterval )
    37. linesY.append(QLineF(rect.left(), y, rect.right(), y));
    38.  
    39. painter->drawLines(linesX.data(), linesX.size());
    40. painter->drawLines(linesY.data(), linesY.size());
    41. }
    To copy to clipboard, switch view to plain text mode 
    On executing this code shows me the following error"virtual void QGraphicsView:drawBackground() is protected"......
    i am nt getting that where's the error......

  11. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to make a Grid on QGraphicscene

    Quote Originally Posted by mind_freak View Post
    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.

Similar Threads

  1. fit QGraphicscene to QGraphicsview
    By nataly in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2010, 15:05
  2. Qgraphicscene - rendering
    By jsmith in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2009, 13:08
  3. QGraphicScene to stream
    By phannent in forum Newbie
    Replies: 2
    Last Post: 28th June 2008, 09:43
  4. QGraphicScene MousePressEvent
    By Spitz in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2007, 21:46
  5. Custom objects in a QGraphicScene
    By draand in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 11:31

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.