|
- // 小方块类
- class OneBox : public QGraphicsObject
- {
- public:
- OneBox(const QColor &color = Qt::red);
- QRectF boundingRect() const;
- void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
- QPainterPath shape() const;
- private:
- QColor brushColor;
- };
- [code]OneBox::OneBox(const QColor &color):brushColor(color)
- {
-
- }
- QRectF OneBox::boundingRect()const
- {
- qreal penWidth=1;
- return QRectF(-10-penWidth/2,-10-penWidth/2,
- 20+penWidth,20+penWidth);
-
- }
- void OneBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
- {
- painter->drawPixmap(-10,-10,20,20,QPixmap(":/images/box.gif"));
- painter->setBrush(brushColor);
- QColor penColor=brushColor;
- penColor.setAlpha(20);
- painter->setPen(penColor);
- painter->drawRect(-10,-10,20,20);
- }
-
- QPainterPath OneBox::shape()const
- {
- QPainterPath path;
- path.addRect(-9.5,-9.5,19,19);
- return path;
- }
复制代码 [/code] |
|