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平移函数,那么在视图中绘制出项的时候回向右下方偏移。

你可能感兴趣的:(Qt_Linux,qt)