内容表

上一话题

QGraphicsDropShadowEffect

下一话题

QGraphicsEllipseItem

QGraphicsEffect

QGraphicsEffect class is the base class for all graphics effects. 更多

Inheritance diagram of PySide2.QtWidgets.QGraphicsEffect

继承者: QGraphicsBlurEffect , QGraphicsColorizeEffect , QGraphicsDropShadowEffect , QGraphicsOpacityEffect

4.6 版新增。

概要

函数

虚函数

信号

详细描述

Effects alter the appearance of elements by hooking into the rendering pipeline and operating between the source (e.g., a QGraphicsPixmapItem ) and the destination device (e.g., QGraphicsView ‘s viewport). Effects can be disabled by calling setEnabled (false). If effects are disabled, the source is rendered directly.

To add a visual effect to a QGraphicsItem , for example, you can use one of the standard effects, or alternately, create your own effect by creating a subclass of QGraphicsEffect . The effect can then be installed on the item using setGraphicsEffect() .

Qt provides the following standard effects:

../../_images/graphicseffect-plain.png
../../_images/graphicseffect-blur.png ../../_images/graphicseffect-colorize.png
../../_images/graphicseffect-opacity.png ../../_images/graphicseffect-drop-shadow.png
../../_images/graphicseffect-widget.png

For more information on how to use each effect, refer to the specific effect’s documentation.

To create your own custom effect, create a subclass of QGraphicsEffect (or any other existing effects) and reimplement the virtual function draw() . This function is called whenever the effect needs to redraw. The draw() function takes the painter with which to draw as an argument. For more information, refer to the documenation for draw() . In the draw() function you can call sourcePixmap() to get a pixmap of the graphics effect source which you can then process.

If your effect changes, use update() to request for a redraw. If your custom effect changes the bounding rectangle of the source, e.g., a radial glow effect may need to apply an extra margin, you can reimplement the virtual boundingRectFor() function, and call updateBoundingRect() to notify the framework whenever this rectangle changes. The virtual sourceChanged() function is called to notify the effects that the source has changed in some way - e.g., if the source is a QGraphicsRectItem and its rectangle parameters have changed.

class QGraphicsEffect ( [ parent=None ] )
param parent

QObject

构造新 QGraphicsEffect instance having the specified parent .

PySide2.QtWidgets.QGraphicsEffect. ChangeFlag

This enum describes what has changed in QGraphicsEffectSource.

常量

描述

QGraphicsEffect.SourceAttached

The effect is installed on a source.

QGraphicsEffect.SourceDetached

The effect is uninstalled on a source.

QGraphicsEffect.SourceBoundingRectChanged

The bounding rect of the source has changed.

QGraphicsEffect.SourceInvalidated

The visual appearance of the source has changed.

PySide2.QtWidgets.QGraphicsEffect. PixmapPadMode

This enum describes how the pixmap returned from sourcePixmap should be padded.

常量

描述

QGraphicsEffect.NoPad

The pixmap should not receive any additional padding.

QGraphicsEffect.PadToTransparentBorder

The pixmap should be padded to ensure it has a completely transparent border.

QGraphicsEffect.PadToEffectiveBoundingRect

The pixmap should be padded to match the effective bounding rectangle of the effect.

PySide2.QtWidgets.QGraphicsEffect. boundingRect ( )
返回类型

QRectF

Returns the effective bounding rectangle for this effect, i.e., the bounding rectangle of the source in device coordinates, adjusted by any margins applied by the effect itself.

PySide2.QtWidgets.QGraphicsEffect. boundingRectFor ( sourceRect )
参数

sourceRect QRectF

返回类型

QRectF

Returns the effective bounding rectangle for this effect, given the provided rect in the device coordinates. When writing you own custom effect, you must call updateBoundingRect() whenever any parameters are changed that may cause this this function to return a different value.

PySide2.QtWidgets.QGraphicsEffect. draw ( painter )
参数

painter QPainter

This pure virtual function draws the effect and is called whenever the source needs to be drawn.

Reimplement this function in a QGraphicsEffect subclass to provide the effect’s drawing implementation, using painter .

例如:

def draw(self, painter):
    # ...
    offset = QPoint()
    if self.sourceIsPixmap():
        # No point in drawing in device coordinates (pixmap will be scaled anyways).
        pixmap = sourcePixmap(Qt.LogicalCoordinates, offset)
        ...
        painter.drawPixmap(offset, pixmap)
    else:
        # Draw pixmap in device coordinates to avoid pixmap scaling
        pixmap = sourcePixmap(Qt.DeviceCoordinates, offset)
        painter.setWorldTransform(QTransform())
        # ...
        painter.drawPixmap(offset, pixmap)
    # ...
											

This function should not be called explicitly by the user, since it is meant for reimplementation purposes only.

PySide2.QtWidgets.QGraphicsEffect. drawSource ( painter )
参数

painter QPainter

Draws the source directly using the given painter .

This function should only be called from draw() .

例如:

def draw(self, painter):
    # Fully opaque draw directly without going through a pixmap.
    if qFuzzyCompare(self.opacity, 1):
        drawSource(painter)
        return
    # ...
											

另请参阅

draw()

PySide2.QtWidgets.QGraphicsEffect. enabledChanged ( enabled )
参数

enabled bool

PySide2.QtWidgets.QGraphicsEffect. isEnabled ( )
返回类型

bool

PySide2.QtWidgets.QGraphicsEffect. setEnabled ( enable )
参数

enable bool

另请参阅

isEnabled()

PySide2.QtWidgets.QGraphicsEffect. sourceBoundingRect ( [ system=Qt.LogicalCoordinates ] )
参数

system CoordinateSystem

返回类型

QRectF

Returns the bounding rectangle of the source mapped to the given system .

调用此函数采用 DeviceCoordinates outside of draw() will give undefined results, as there is no device context available.

另请参阅

draw()

PySide2.QtWidgets.QGraphicsEffect. sourceChanged ( flags )
参数

flags ChangeFlags

此虚拟函数被调用由 QGraphicsEffect to notify the effect that the source has changed. If the effect applies any cache, then this cache must be purged in order to reflect the new appearance of the source.

flags describes what has changed.

PySide2.QtWidgets.QGraphicsEffect. sourceIsPixmap ( )
返回类型

bool

返回 true if the source effectively is a pixmap, e.g., a QGraphicsPixmapItem .

This function is useful for optimization purposes. For instance, there’s no point in drawing the source in device coordinates to avoid pixmap scaling if this function returns true - the source pixmap will be scaled anyways.

PySide2.QtWidgets.QGraphicsEffect. sourcePixmap ( [ system=Qt.LogicalCoordinates [ , offset=None [ , mode=PadToEffectiveBoundingRect ] ] ] )
参数
返回类型

QPixmap

Returns a pixmap with the source painted into it.

system specifies which coordinate system to be used for the source. The optional offset parameter returns the offset where the pixmap should be painted at using the current painter. For control on how the pixmap is padded use the mode 参数。

The returned pixmap is clipped to the current painter’s device rectangle when system is DeviceCoordinates .

调用此函数采用 DeviceCoordinates outside of draw() will give undefined results, as there is no device context available.

PySide2.QtWidgets.QGraphicsEffect. update ( )

Schedules a redraw of the effect. Call this function whenever the effect needs to be redrawn. This function does not trigger a redraw of the source.

PySide2.QtWidgets.QGraphicsEffect. updateBoundingRect ( )

This function notifies the effect framework when the effect’s bounding rectangle has changed. As a custom effect author, you must call this function whenever you change any parameters that will cause the virtual boundingRectFor() function to return a different value.

This function will call update() if this is necessary.