Home · All Classes · All Functions · Overviews

QGraphicsEffect Class Reference
[
QtGui module]

The QGraphicsEffect class is the base class for all graphics effects. More...

 #include <QGraphicsEffect>

Inherits QObject.

Inherited by QGraphicsBloomEffect, QGraphicsBlurEffect, QGraphicsColorizeEffect, QGraphicsDropShadowEffect, QGraphicsGrayscaleEffect, QGraphicsOpacityEffect, and QGraphicsPixelizeEffect.

This class was introduced in Qt 4.6.


Public Types

enum ChangeFlag { SourceAttached, SourceDetached, SourceBoundingRectChanged, SourceInvalidated }
flags ChangeFlags

Properties


Public Functions

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

Public Slots

void setEnabled ( bool enable )
void update ()

Signals

void enabledChanged ( bool enabled )

Protected Functions

virtual void draw ( QPainter * painter, QGraphicsEffectSource * source ) = 0
virtual void sourceChanged ( ChangeFlags flags )
void updateBoundingRect ()

Additional Inherited Members


Detailed Description

The QGraphicsEffect class is the base class for all graphics effects.

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 QGraphicsItem::setGraphicsEffect().

Qt provides the following standard effects:

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 accepts two arguments: the painter and a pointer to the source (QGraphicsEffectSource). The source provides extra context information, such as a pointer to the item that is rendering the effect, any cached pixmap data, or the device rectangle bounds. For more information, refer to the documenation for draw(). To obtain a pointer to the current source, simply call source().

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 sourceBoundingRectChanged() function is called to notify the effects that the source's bounding rectangle has changed - e.g., if the source is a QGraphicsRectItem and its rectangle parameters have changed.

See also QGraphicsItem::setGraphicsEffect(), QWidget::setGraphicsEffect(), and QGraphicsEffectSource.


Member Type Documentation

enum QGraphicsEffect::ChangeFlag
flags QGraphicsEffect::ChangeFlags

This enum describes what has changed in QGraphicsEffectSource.

ConstantValueDescription
QGraphicsEffect::SourceAttached0x1The effect is installed on a source.
QGraphicsEffect::SourceDetached0x2The effect is uninstalled on a source.
QGraphicsEffect::SourceBoundingRectChanged0x4The bounding rect of the source has changed.
QGraphicsEffect::SourceInvalidated0x8The visual appearance of the source has changed.

The ChangeFlags type is a typedef for QFlags<ChangeFlag>. It stores an OR combination of ChangeFlag values.


Property Documentation

enabled : bool

This property holds whether the effect is enabled or not.

If an effect is disabled, the source will be rendered with as normal, with no interference from the effect. If the effect is enabled, the source will be rendered with the effect applied.

This property is enabled by default.

Using this property, you can disable certain effects on slow platforms, in order to ensure that the user interface is responsive.

Access functions:

bool isEnabled () const
void setEnabled ( bool enable )

Notifier signal:

void enabledChanged ( bool enabled )

Member Function Documentation

QGraphicsEffect::QGraphicsEffect ( QObject * parent = 0 )

Constructs a new QGraphicsEffect instance having the specified parent.

QGraphicsEffect::~QGraphicsEffect ()   [virtual]

Removes the effect from the source, and destroys the graphics effect.

QRectF QGraphicsEffect::boundingRect () const

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

See also boundingRectFor() and updateBoundingRect().

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

Returns the bounding rectangle for this effect, given the provided source rect. 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.

See also boundingRect().

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

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. The source parameter is provided for convenience; its value is the same as source().

For example:

 MyGraphicsEffect::draw(QPainter *painter, QGraphicsEffectSource *source)
 {
     ...
     QPoint offset;
     if (source->isPixmap()) {
         // No point in drawing in device coordinates (pixmap will be scaled anyways).
         const QPixmap pixmap = source->pixmap(Qt::LogicalCoordinates, &offset);
         ...
         painter->drawPixmap(offset, pixmap);
     } else {
         // Draw pixmap in device coordinates to avoid pixmap scaling;
         const QPixmap pixmap = source->pixmap(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.

See also QGraphicsEffectSource.

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

This signal is emitted whenever the effect is enabled or disabled. The enabled parameter holds the effects's new enabled state.

See also isEnabled().

QGraphicsEffectSource * QGraphicsEffect::source () const

Returns a pointer to the source, which provides extra context information that can be useful for the effect.

See also draw().

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

This virtual function is called by 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.

The flags describes what has changed.

void QGraphicsEffect::update ()   [slot]

Schedules a redraw of the source. Call this function whenever the source needs to be redrawn.

This convenience function is equivalent to calling QGraphicsEffectSource::update().

See also updateBoundingRect() and QGraphicsEffectSource::update().

void QGraphicsEffect::updateBoundingRect ()   [protected]

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.

See also boundingRectFor() and boundingRect().


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt 4.6.0