PDA

View Full Version : problem with Qgraphics classes



kokeroulis
18th August 2010, 13:03
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....


main.cpp

#include <QtGui/QApplication>
#include "board.h"
#include <QGraphicsView>

int main(int argc, char** argv)
{
QApplication app(argc, argv);
qscene scene;

QGraphicsView view(&scene);
view.show();
return app.exec();
}



board.h

#ifndef board_H
#define board_H

#include <QGraphicsItem>
#include "qscene.h"
//#include <QGraphicsScene>
//#include <QGraphicsRectItem>

class board : public QGraphicsItem//, public qscene
{

public:
virtual QRectF boundingRect() const;
virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*);
board(qscene *scene);

//virtual ~checkers2();
};
#endif



board.cpp

#include "board.h"



board::board(qscene *scene)
{
for(int i=0; i<=7; i++) {
int x =-390, y=-310;
y = y +(i*50);
for(int j=0; j<=7; j++) {
QGraphicsRectItem *rectb = new QGraphicsRectItem(this);
if (i%2 !=0) {
if(j%2 == 0) {
rectb->setRect(x, y, 50, 50);
rectb->setBrush(QBrush(Qt::white));
}
else {
rectb->setRect(x, y, 50, 50);
rectb->setBrush(QBrush(Qt::black));
}}
else {
if(j%2 == 0) {
rectb->setRect(x, y, 50, 50);
rectb->setBrush(QBrush(Qt::black));
}
else {
rectb->setRect(x, y, 50, 50);
rectb->setBrush(QBrush(Qt::white));
}
}
rectb->setZValue(1);

scene->addItem(rectb);
x = x+50;
}
}
}

QRectF board::boundingRect() const
{

}
void board::paint(QPainter* painter , const QStyleOptionGraphicsItem*
options, QWidget* widget)
{
foreach( QGraphicsItem *i, childItems()){
i->paint(painter, options , widget);}}

#include "board.moc"



qscene.h

#ifndef qscene_H
#define qscene_H
//#include <QGraphicsItem>
#include <QtGui/QGraphicsView>
#include <QGraphicsScene>
class qscene : public QGraphicsScene//,public QGraphicsView
{

public:
qscene();
//virtual ~checkers2();
};

#endif



qscene.cpp

#include "qscene.h"
//#include <QGraphicsScene>
#include "board.h"




qscene::qscene()
{

QGraphicsScene *scene = new QGraphicsScene(this);
scene->setSceneRect(QRectF(0,0,10,10));
board terrain(qscene *scene);
}



#include "qscene.moc"

aamer4yu
18th August 2010, 13:34
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..

kokeroulis
18th August 2010, 16:50
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?