QGraphicsEffect Class

QGraphicsEffect 类是所有图形效果的基类。更多

头文件: #include <QGraphicsEffect>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QObject
继承者:

QGraphicsBlurEffect,QGraphicsColorizeEffect,QGraphicsDropShadowEffect, 以及QGraphicsOpacityEffect

公共类型

enum ChangeFlag { SourceAttached, SourceDetached, SourceBoundingRectChanged, SourceInvalidated }
flags ChangeFlags
enum PixmapPadMode { NoPad, PadToTransparentBorder, PadToEffectiveBoundingRect }

属性

公共功能

QGraphicsEffect(QObject *parent = nullptr)
virtual ~QGraphicsEffect()
QRectF boundingRect() const
virtual QRectF boundingRectFor(const QRectF &rect) const
bool isEnabled() const

公共插槽

void setEnabled(bool enable)
void update()

信号

void enabledChanged(bool enabled)

受保护功能

virtual void draw(QPainter *painter) = 0
void drawSource(QPainter *painter)
QRectF sourceBoundingRect(Qt::CoordinateSystem system = Qt::LogicalCoordinates) const
virtual void sourceChanged(QGraphicsEffect::ChangeFlags flags)
bool sourceIsPixmap() const
QPixmap sourcePixmap(Qt::CoordinateSystem system = Qt::LogicalCoordinates, QPoint *offset = nullptr, QGraphicsEffect::PixmapPadMode mode = PadToEffectiveBoundingRect) const
void updateBoundingRect()

详细说明

特效通过挂钩渲染管道并在源设备(如QGraphicsPixmapItem )和目标设备(如QGraphicsView 的视口)之间运行来改变元素的外观。可以通过调用setEnabled(false) 来禁用特效。如果禁用特效,则直接渲染源。

例如,要在QGraphicsItem 中添加视觉效果,可以使用标准效果之一,或者通过创建 QGraphicsEffect 的子类来创建自己的效果。然后就可以使用QGraphicsItem::setGraphicsEffect() 将特效安装到项目上。

Qt 提供了以下标准效果:

有关如何使用每种特效的详细信息,请参阅特定特效的文档。

要创建自己的自定义特效,请创建 QGraphicsEffect(或任何其他现有特效)的子类,并重新实现虚拟函数draw() 。每当效果需要重绘时,该函数就会被调用。draw() 函数将绘制所用的绘制器作为参数。更多信息,请参阅draw() 文档。在draw() 函数中,您可以调用sourcePixmap() 来获取图形效果源的像素图,然后对其进行处理。

如果效果发生变化,请使用update() 请求重绘。如果您的自定义特效改变了源的边界矩形,例如,径向发光特效可能需要应用额外的边距,您可以重新实现虚拟boundingRectFor() 函数,并调用updateBoundingRect() 在矩形发生变化时通知框架。调用虚拟sourceChanged() 函数是为了通知特效,源已以某种方式发生变化,例如,如果源是QGraphicsRectItem ,且其矩形参数已发生变化。

另请参阅 QGraphicsItem::setGraphicsEffect() 和QWidget::setGraphicsEffect()。

成员类型文档

枚举 QGraphicsEffect::ChangeFlag
flags QGraphicsEffect::ChangeFlags

此枚举描述了 QGraphicsEffectSource 中的更改。

常量描述
QGraphicsEffect::SourceAttached0x1在源上安装了效果。
QGraphicsEffect::SourceDetached0x2效果已从源上卸载。
QGraphicsEffect::SourceBoundingRectChanged0x4源的边界矩形已更改。
QGraphicsEffect::SourceInvalidated0x8源的视觉外观已更改。

ChangeFlags 类型是QFlags<ChangeFlag> 的类型定义。它存储了 ChangeFlag 值的 OR 组合。

enum QGraphicsEffect::PixmapPadMode

该枚举描述了从sourcePixmap 返回的像素图的填充方式。

常数说明
QGraphicsEffect::NoPad0像素图不应添加任何填充。
QGraphicsEffect::PadToTransparentBorder1应填充像素图以确保其具有完全透明的边框。
QGraphicsEffect::PadToEffectiveBoundingRect2应填充像素图以匹配效果的有效边界矩形。

属性文档

enabled : bool

此属性表示效果是否启用。

如果效果被禁用,信号源将以正常方式呈现,不会受到效果的干扰。如果效果已启用,则会在应用效果后渲染信号源。

该属性默认为启用。

使用此属性,可以在慢速平台上禁用某些特效,以确保用户界面反应灵敏。

访问功能:

bool isEnabled() const
void setEnabled(bool enable)

Notifier 信号:

void enabledChanged(bool enabled)

成员函数 文档

QGraphicsEffect::QGraphicsEffect(QObject *parent = nullptr)

构建一个新的 QGraphicsEffect 实例,该实例具有指定的parent

[virtual noexcept] QGraphicsEffect::~QGraphicsEffect()

从信号源中移除效果,并破坏 Graphical Effects。

QRectF QGraphicsEffect::boundingRect() const

返回此特效的有效边界矩形,即以设备坐标表示的源边界矩形,并根据特效本身应用的任何边距进行调整。

另请参阅 boundingRectFor() 和updateBoundingRect()。

[virtual] QRectF QGraphicsEffect::boundingRectFor(const QRectF &rect) const

根据所提供的rect 设备坐标,返回此特效的有效边界矩形。在编写自己的自定义特效时,如果参数发生变化,可能导致此函数返回不同的值,这时必须调用updateBoundingRect() 。

另请参阅 sourceBoundingRect()。

[pure virtual protected] void QGraphicsEffect::draw(QPainter *painter)

这个纯虚拟函数用于绘制效果,每当需要绘制源时都会被调用。

请在QGraphicsEffect 子类中重新实现该函数,以便使用painter 实现效果绘制。

例如

MyGraphicsEffect::draw(QPainter *painter)
{
    ...
    QPoint offset;
    if (sourceIsPixmap()) {
        // No point in drawing in device coordinates (pixmap will be scaled anyways).
        const QPixmap pixmap = sourcePixmap(Qt::LogicalCoordinates, &offset);
        ...
        painter->drawPixmap(offset, pixmap);
    } else {
        // Draw pixmap in device coordinates to avoid pixmap scaling;
        const QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset);
        painter->setWorldTransform(QTransform());
        ...
        painter->drawPixmap(offset, pixmap);
    }
    ...
}

用户不应明确调用此函数,因为它仅用于重新实现。

[protected] void QGraphicsEffect::drawSource(QPainter *painter)

使用给定的painter 直接绘制源文件。

该函数只能从QGraphicsEffect::draw() 中调用。

例如

MyGraphicsOpacityEffect::draw(QPainter *painter)
{
    // Fully opaque; draw directly without going through a pixmap.
    if (qFuzzyCompare(m_opacity, 1)) {
        drawSource(painter);
        return;
    }
    ...
}

另请参见 QGraphicsEffect::draw().

[signal] void QGraphicsEffect::enabledChanged(bool enabled)

每当特效启用或禁用时,都会发出该信号。enabled 参数包含特效的新启用状态。

注: 属性enabled 的通知信号。

另请参阅 isEnabled().

[protected] QRectF QGraphicsEffect::sourceBoundingRect(Qt::CoordinateSystem system = Qt::LogicalCoordinates) const

返回映射到给定system 的源的边界矩形。

QGraphicsEffect::draw() 之外使用Qt::DeviceCoordinates 调用此函数将产生未定义的结果,因为没有可用的设备上下文。

另请参见 draw()。

[virtual protected] void QGraphicsEffect::sourceChanged(QGraphicsEffect::ChangeFlags flags)

QGraphicsEffect 调用此虚函数,通知效果源已更改。如果效果应用了缓存,则必须清除缓存以反映源的新外观。

flags 会描述发生了哪些变化。

[protected] bool QGraphicsEffect::sourceIsPixmap() const

如果源有效为像素图,如QGraphicsPixmapItem ,则返回true

此函数对优化非常有用。例如,如果该函数返回true ,那么以设备坐标绘制源以避免像素图缩放就没有意义了--无论如何,源像素图都会被缩放。

[protected] QPixmap QGraphicsEffect::sourcePixmap(Qt::CoordinateSystem system = Qt::LogicalCoordinates, QPoint *offset = nullptr, QGraphicsEffect::PixmapPadMode mode = PadToEffectiveBoundingRect) const

返回绘有源图像的像素图。

system 指定了源要使用的坐标系。可选的offset 参数会返回使用当前绘制器绘制像素图时的偏移量。要控制像素图的填充方式,请使用mode 参数。

systemQt::DeviceCoordinates 时,返回的像素图将被剪切到当前绘制器的设备矩形内。

QGraphicsEffect::draw() 之外使用Qt::DeviceCoordinates 调用此函数将产生未定义的结果,因为没有可用的设备上下文。

另请参阅 draw() 和boundingRect()。

[slot] void QGraphicsEffect::update()

安排重绘效果。在需要重绘效果时调用此函数。此函数不会触发源的重绘。

另请参阅 updateBoundingRect()。

[protected] void QGraphicsEffect::updateBoundingRect()

当特效的边界矩形发生变化时,该函数会通知特效框架。作为自定义特效的制作者,每当您更改任何会导致虚拟boundingRectFor() 函数返回不同值的参数时,都必须调用此函数。

如有必要,该函数将调用update() 。

另请参阅 boundingRectFor()、boundingRect() 和sourceBoundingRect()。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.