Qt GraphicsItem绘图相关

笔记:

1.paint函数

void QGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) [pure virtual]
This function, which is usually called by QGraphicsView, paints the contents of an item in local coordinates.

Reimplement this function in a QGraphicsItem subclass to provide the item's painting implementation, using painter. The option parameter provides style options for the item, such as its state, exposed area and its level-of-detail hints. The widget argument is optional. If provided, it points to the widget that is being painted on; otherwise, it is 0. For cached painting, widget is always 0.

 void RoundRectItem::paint(QPainter *painter,
                           const QStyleOptionGraphicsItem *option,
                           QWidget *widget)
 {
     painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
 }
The painter's pen is 0-width by default, and its pen is initialized to the QPalette::Text brush from the paint device's palette. The brush is initialized to QPalette::Window.

Make sure to constrain all painting inside the boundaries of boundingRect() to avoid rendering artifacts (as QGraphicsView does not clip the painter for you). In particular, when QPainter renders the outline of a shape using an assigned QPen, half of the outline will be drawn outside, and half inside, the shape you're rendering (e.g., with a pen width of 2 units, you must draw outlines 1 unit inside boundingRect()). QGraphicsItem does not support use of cosmetic pens with a non-zero width.

All painting is done in local coordinates.

这个函数是实现QGraphics中任意Item绘图必须自己实现的纯虚函数:

    painter是QPainter类的对象,可以直接用其来绘制想绘制的图形

    option是QStyleOptionGraphiItem的对象,可用其来获取当前Item的一些相关信息(例如:option->state)

    widget是QWidget类的对象,为当前绘制的父对象,即绘制的界面为widget界面,默认为0(在Item的父对象,若无父对象就在item所处的Scene中绘制)


2.boundingRect()函数

QRectF QGraphicsItem::boundingRect () const [pure virtual]

This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item's bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.

Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation.

If you want to change the item's bounding rectangle, you must first call prepareGeometryChange(). This notifies the scene of the imminent change, so that its can update its item geometry index; otherwise, the scene will be unaware of the item's new geometry, and the results are undefined (typically, rendering artifacts are left around in the view).

Reimplement this function to let QGraphicsView determine what parts of the widget, if any, need to be redrawn.

Note: For shapes that paint an outline / stroke, it is important to include half the pen width in the bounding rect. It is not necessary to compensate for antialiasing, though.

这也是一个必须自己实现的纯虚函数,该函数提供你将要绘图的矩形,即paint函数绘制的图形是处于该函数返回的QRectF对象中


3.shape()

QPainterPath QGraphicsItem::shape () const [virtual]
Returns the shape of this item as a QPainterPath in local coordinates. The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.

The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items. For example, a round item may choose to return an elliptic shape for better collision detection. For example:

 QPainterPath RoundItem::shape() const
 {
     QPainterPath path;
     path.addEllipse(boundingRect());
     return path;
 }

The outline of a shape can vary depending on the width and style of the pen used when drawing. If you want to include this outline in the item's shape, you can create a shape from the stroke using QPainterPathStroker.

This function is called by the default implementations of contains() and collidesWithPath().

这还是一个必须自己实现的虚函数,该函数返会你一个线的路径,可以使用它来使你的矩形变得温柔。


猜测:

QGraphicsView调用boundingRect(),决定是否需要绘制项。而boundingRect返回的是一个以左上角为原点的矩形框,在视图中将分配这样一个矩形给项绘制其本身。而在QGraphicsItem::setPos(QPoint &point)函数中实现的是将item项的坐标原点(中心)设置在boundingRect这个矩形的中心。然后paint()绘制的图形将根据项坐标绘制item。所以如果boundingRect返回的矩形在项坐标中不是关于原点对称的时候,即boundingRect()函数返回的矩形没有经过translate平移函数,那么在视图中绘制出项的时候回向右下方偏移。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值