Results 1 to 3 of 3

Thread: problem with Qgraphics classes

  1. #1
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default problem with Qgraphics classes

    hello

    This my first time that i am trying to create something with qt...So probably i have made a very silly mistake but i don't know how to fix it, so i need your help. I want to create the terrain for a chess game... My code compiles properly but the problem is that it doesn't paint the board as it is supposed to do....Here is the files which i use to create the program....

    Qt Code:
    1. main.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "board.h"
    3. #include <QGraphicsView>
    4.  
    5. int main(int argc, char** argv)
    6. {
    7. QApplication app(argc, argv);
    8. qscene scene;
    9.  
    10. QGraphicsView view(&scene);
    11. view.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. board.h
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef board_H
    2. #define board_H
    3.  
    4. #include <QGraphicsItem>
    5. #include "qscene.h"
    6. //#include <QGraphicsScene>
    7. //#include <QGraphicsRectItem>
    8.  
    9. class board : public QGraphicsItem//, public qscene
    10. {
    11.  
    12. public:
    13. virtual QRectF boundingRect() const;
    14. virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);
    15. board(qscene *scene);
    16.  
    17. //virtual ~checkers2();
    18. };
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. board.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "board.h"
    2.  
    3.  
    4.  
    5. board::board(qscene *scene)
    6. {
    7. for(int i=0; i<=7; i++) {
    8. int x =-390, y=-310;
    9. y = y +(i*50);
    10. for(int j=0; j<=7; j++) {
    11. if (i%2 !=0) {
    12. if(j%2 == 0) {
    13. rectb->setRect(x, y, 50, 50);
    14. rectb->setBrush(QBrush(Qt::white));
    15. }
    16. else {
    17. rectb->setRect(x, y, 50, 50);
    18. rectb->setBrush(QBrush(Qt::black));
    19. }}
    20. else {
    21. if(j%2 == 0) {
    22. rectb->setRect(x, y, 50, 50);
    23. rectb->setBrush(QBrush(Qt::black));
    24. }
    25. else {
    26. rectb->setRect(x, y, 50, 50);
    27. rectb->setBrush(QBrush(Qt::white));
    28. }
    29. }
    30. rectb->setZValue(1);
    31.  
    32. scene->addItem(rectb);
    33. x = x+50;
    34. }
    35. }
    36. }
    37.  
    38. QRectF board::boundingRect() const
    39. {
    40.  
    41. }
    42. void board::paint(QPainter* painter , const QStyleOptionGraphicsItem*
    43. options, QWidget* widget)
    44. {
    45. foreach( QGraphicsItem *i, childItems()){
    46. i->paint(painter, options , widget);}}
    47.  
    48. #include "board.moc"
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. qscene.h
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef qscene_H
    2. #define qscene_H
    3. //#include <QGraphicsItem>
    4. #include <QtGui/QGraphicsView>
    5. #include <QGraphicsScene>
    6. class qscene : public QGraphicsScene//,public QGraphicsView
    7. {
    8.  
    9. public:
    10. qscene();
    11. //virtual ~checkers2();
    12. };
    13.  
    14. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. qscene.cpp
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "qscene.h"
    2. //#include <QGraphicsScene>
    3. #include "board.h"
    4.  
    5.  
    6.  
    7.  
    8. qscene::qscene()
    9. {
    10.  
    11. QGraphicsScene *scene = new QGraphicsScene(this);
    12. scene->setSceneRect(QRectF(0,0,10,10));
    13. board terrain(qscene *scene);
    14. }
    15.  
    16.  
    17.  
    18. #include "qscene.moc"
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: problem with Qgraphics classes

    From the code I guess you are confused about the graphics view framework.
    scene->setSceneRect(QRectF(0,0,10,10)); ---> you didnt want your whole chess board to be 10,10 square,,, did you ?

    Few points -
    1) You create a scene.
    2) You add items to scene ( which you havent done. If you say you wrote board terrain(qscene *scene); think again... its a mere function declaration !! )
    3) You set scene to a view.
    4) show the view..

    Also for simply showing chess board you could simply draw inside a widget, without using the graphics view framework.
    Then later when you want to add items in the chess.. ( the players, animals etc) then you can create a scene,,, draw the squared as background of the scene. Then create items for chess board items and place them in the scene.

    Some other points -
    board::board(qscene *scene)
    {
    .....
    QGraphicsRectItem *rectb = new QGraphicsRectItem(this);
    You are inheriting board from QgraphicsItem.. you need not have rectb inside it...

    Just check some example in Qt Demos and you will be more clear in what I am saying..

  3. #3
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problem with Qgraphics classes

    Quote Originally Posted by aamer4yu View Post
    From the code I guess you are confused about the graphics view framework.
    scene->setSceneRect(QRectF(0,0,10,10)); ---> you didnt want your whole chess board to be 10,10 square,,, did you ?


    2) You add items to scene ( which you havent done. If you say you wrote board terrain(qscene *scene); think again... its a mere function declaration !! )
    I want to use the qgraphics framework for board and for the chess items, because i don't want to make it very complicated...so except from the very very small board, then how an i fix the 2?

Similar Threads

  1. Animation and QGraphics
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2009, 00:31
  2. Build problem with more then 4 classes
    By ferdikoomen in forum Qt Programming
    Replies: 3
    Last Post: 14th March 2009, 13:30
  3. Qgraphics View
    By amagdy.ibrahim in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2008, 10:10
  4. Replies: 2
    Last Post: 20th September 2007, 12:27
  5. QGraphics view problem
    By kiranraj in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 21:28

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.