I've started implementing my own subclass of QGraphicsItem:
#ifndef PAWN_H
#define PAWN_H
#include <QtGui>
{
private:
static const int FIGURE_SIZE = 50;
public:
};
#endif // PAWN_H
#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
To copy to clipboard, switch view to plain text mode
QRectF Pawn
::boundingRect() const {
return QRectF(this
->x
(), this
->y
(), FIGURE_SIZE, FIGURE_SIZE
);
}
{
painter->drawRect(boundingRect());
}
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());
}
To copy to clipboard, switch view to plain text mode
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);
Pawn* first = new Pawn();
first->setPos(0, 0);
Pawn* second = new Pawn();
second->setPos(0, 50);
mainScene.addItem(first);
mainScene.addItem(second);
To copy to clipboard, switch view to plain text mode
It looks as follows:
screen.png
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.
Bookmarks