Azras
30th May 2013, 12:31
I've started implementing my own subclass of QGraphicsItem:
#ifndef PAWN_H
#define PAWN_H
#include <QtGui>
class Pawn : public QGraphicsItem
{
private:
static const int FIGURE_SIZE = 50;
public:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // PAWN_H
QRectF Pawn::boundingRect() const
{
return QRectF(this->x(), this->y(), FIGURE_SIZE, FIGURE_SIZE);
}
void Pawn::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(boundingRect());
}
Then I've added two items to QGraphicsScene:
Pawn* first = new Pawn();
first->setPos(0, 0);
Pawn* second = new Pawn();
second->setPos(0, 50);
mainScene.addItem(first);
mainScene.addItem(second);
It looks as follows:
9088
Why there is a gap between these two rectangles? Top left vertex of first rectangle is (0, 0) and it's heigth is 50. Top left vertex of second rectangle is (0, 50). They should have a common edge.
#ifndef PAWN_H
#define PAWN_H
#include <QtGui>
class Pawn : public QGraphicsItem
{
private:
static const int FIGURE_SIZE = 50;
public:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // PAWN_H
QRectF Pawn::boundingRect() const
{
return QRectF(this->x(), this->y(), FIGURE_SIZE, FIGURE_SIZE);
}
void Pawn::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(boundingRect());
}
Then I've added two items to QGraphicsScene:
Pawn* first = new Pawn();
first->setPos(0, 0);
Pawn* second = new Pawn();
second->setPos(0, 50);
mainScene.addItem(first);
mainScene.addItem(second);
It looks as follows:
9088
Why there is a gap between these two rectangles? Top left vertex of first rectangle is (0, 0) and it's heigth is 50. Top left vertex of second rectangle is (0, 50). They should have a common edge.