學(xué)習(xí)筆記:Qt GraphicsItem繪圖相關(guān)
筆記:
1.paint函數(shù)
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.
這個函數(shù)是實(shí)現(xiàn)QGraphics中任意Item繪圖必須自己實(shí)現(xiàn)的純虛函數(shù):
??? painter是QPainter類的對象,可以直接用其來繪制想繪制的圖形
??? option是QStyleOptionGraphiItem的對象,可用其來獲取當(dāng)前Item的一些相關(guān)信息(例如:option->state)
??? widget是QWidget類的對象,為當(dāng)前繪制的父對象,即繪制的界面為widget界面,默認(rèn)為0(在Item的父對象,若無父對象就在item所處的Scene中繪制)
2.boundingRect()函數(shù)
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.
這也是一個必須自己實(shí)現(xiàn)的純虛函數(shù),該函數(shù)提供你將要繪圖的矩形,即paint函數(shù)繪制的圖形是處于該函數(shù)返回的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().
這還是一個必須自己實(shí)現(xiàn)的虛函數(shù),該函數(shù)返會你一個線的路徑,可以使用它來使你的矩形變得溫柔。
猜測:
QGraphicsView調(diào)用boundingRect(),決定是否需要繪制項(xiàng)。而boundingRect返回的是一個以左上角為原點(diǎn)的矩形框,在視圖中將分配這樣一個矩形給項(xiàng)繪制其本身。而在QGraphicsItem::setPos(QPoint &point)函數(shù)中實(shí)現(xiàn)的是將item項(xiàng)的坐標(biāo)原點(diǎn)(中心)設(shè)置在boundingRect這個矩形的中心。然后paint()繪制的圖形將根據(jù)項(xiàng)坐標(biāo)繪制item。所以如果boundingRect返回的矩形在項(xiàng)坐標(biāo)中不是關(guān)于原點(diǎn)對稱的時候,即boundingRect()函數(shù)返回的矩形沒有經(jīng)過translate平移函數(shù),那么在視圖中繪制出項(xiàng)的時候回向右下方偏移。