QGraphicsEffect Class
QGraphicsEffect 类是所有 Graphical Effects 的基类。更多内容...
| 头文件: | #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 } |
属性
- enabled : bool
公共函数
| 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 提供了以下标准效果:
- QGraphicsBlurEffect - 根据指定的半径对项目进行模糊处理
- QGraphicsDropShadowEffect - 在项目背后渲染一个投影
- QGraphicsColorizeEffect - 以任意指定颜色的色调渲染项目
- QGraphicsOpacityEffect - 以指定不透明度渲染项
| |
|
|
|
|

有关如何使用各效果的更多信息,请参阅相应效果的文档。
若要创建自定义效果,请创建 QGraphicsEffect(或任何其他现有效果)的子类,并重写虚拟函数draw()。每当效果需要重新绘制时,都会调用此函数。draw() 函数将用于绘制的绘制器作为参数传入。 有关更多信息,请参阅draw() 的文档。在draw() 函数中,您可以调用sourcePixmap() 来获取图形效果源的位图,然后对其进行处理。
如果效果发生变化,请使用update() 请求重绘。如果自定义效果改变了源的边界矩形(例如,径向光晕效果可能需要应用额外的边距),您可以重写虚拟函数boundingRectFor(),并在该矩形发生变化时调用updateBoundingRect() 来通知框架。 调用虚函数sourceChanged() 是为了通知效果,源对象已发生某种变化——例如,当源对象是QGraphicsRectItem 且其矩形参数已发生变化时。
另请参阅 QGraphicsItem::setGraphicsEffect() 和QWidget::setGraphicsEffect()。
成员类型文档
enum QGraphicsEffect::ChangeFlag
flags QGraphicsEffect::ChangeFlags
此枚举描述了 QGraphicsEffectSource 中的变更内容。
| 常量 | 值 | 描述 |
|---|---|---|
QGraphicsEffect::SourceAttached | 0x1 | 该效果已安装在源上。 |
QGraphicsEffect::SourceDetached | 0x2 | 该效果已从源上卸载。 |
QGraphicsEffect::SourceBoundingRectChanged | 0x4 | 源的边界矩形已发生变化。 |
QGraphicsEffect::SourceInvalidated | 0x8 | 源的视觉外观已发生变化。 |
ChangeFlags 类型是QFlags<ChangeFlag> 的 typedef。它存储 ChangeFlag 值的按“或”运算组合。
enum QGraphicsEffect::PixmapPadMode
此枚举描述了sourcePixmap 返回的像素图应如何进行填充。
| 常量 | 值 | 描述 |
|---|---|---|
QGraphicsEffect::NoPad | 0 | 该像素图不应进行任何额外填充。 |
QGraphicsEffect::PadToTransparentBorder | 1 | 应为像素图添加填充,以确保其具有完全透明的边框。 |
QGraphicsEffect::PadToEffectiveBoundingRect | 2 | 应为位图添加填充,使其与效果的有效边界矩形相匹配。 |
属性文档
enabled : bool
无论该效果是否启用,此属性均有效。
如果效果被禁用,源内容将照常渲染,不会受到该效果的影响。如果效果被启用,源内容将在应用该效果的情况下进行渲染。
此属性默认处于启用状态。
使用此属性,您可以在性能较弱的平台上禁用某些效果,以确保用户界面的响应性。
访问函数:
| bool | isEnabled() const |
| void | setEnabled(bool enable) |
通知器信号:
| void | enabledChanged(bool enabled) |
成员函数文档
QGraphicsEffect::QGraphicsEffect(QObject *parent = nullptr)
创建一个具有指定parent 的新QGraphicsEffect实例。
[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 。
例如:
void 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()中调用。
例如:
void 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 参数。
当system 为Qt::DeviceCoordinates 时,返回的像素图将被裁剪为当前绘图器的设备矩形。
若在调用QGraphicsEffect::draw() 之前调用此函数,且Qt::DeviceCoordinates 参数值超出 () 的范围,将导致结果未定义,因为此时没有可用的设备上下文。
另请参阅 draw() 和boundingRect()。
[slot] void QGraphicsEffect::update()
安排对该效果进行重绘。每当需要重绘该效果时,请调用此函数。此函数不会触发源内容的重绘。
另请参阅 updateBoundingRect()。
[protected] void QGraphicsEffect::updateBoundingRect()
当效果的边界矩形发生变化时,此函数会通知效果框架。作为自定义效果的作者,每当您修改任何会导致虚拟函数boundingRectFor() 返回不同值的参数时,都必须调用此函数。
如有必要,该函数将调用update()。
另请参阅 boundingRectFor()、boundingRect() 和sourceBoundingRect()。
© 2026 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.




