A sample highlighting QGraphicsEffect
English
| فارسی
While learning Qt, I wrote this sample highlighting graphics effect to be used with rectangular QGraphicsPixmapItem.
..and I use this post just to learn how to publish a wiki page here too.. ;-)
highlight.h
//*''''''
#ifndef HIGHLIGHT_H<br />#define HIGHLIGHT_H
#include <QtGui&gt;
//*''''''
class HighlightEffect : public QGraphicsEffect<br />{<br /> Q_OBJECT<br /> Q_PROPERTY( QColor color READ color WRITE setColor )<br /> Q_PROPERTY( QPointF offset READ offset WRITE setOffset )<br />public:<br /> HighlightEffect( qreal offset = 1.5 );<br /> virtual QRectF boundingRectFor( const QRectF &amp;sourceRect; ) const;<br /> QColor color() const { return mColor;}<br /> void setColor( QColor &amp;color; ) { mColor = color;}<br /> QPointF offset() const { return mOffset;}<br /> void setOffset( QPointF offset ) { mOffset = offset;}
protected:<br /> virtual void draw( QPainter *painter ); // , QGraphicsEffectSource '''source );<br />private:<br /> QColor mColor;<br /> QPointF mOffset;<br />};
<br />//'''
#endif // HIGHLIGHT_H
//*''''''<br />
highlight.cpp
//*''''''
#include "highlight.h&quot;
//*''''''
HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(),<br /> mColor( 255, 255, 0, 128 ), // yellow, semi-transparent<br /> mOffset( offset, offset )<br />{<br />}
QRectF HighlightEffect::boundingRectFor( const QRectF &amp;sourceRect; ) const<br />{<br /> return sourceRect.adjusted( -mOffset.x(), <s>mOffset.y(), mOffset.x(), mOffset.y() );<br />}
<br />void HighlightEffect::draw( QPainter '''painter )<br />{<br /> QPoint offset;<br /> QPixmap pixmap;
<br />// if ( sourceIsPixmap() ) // doesn't seems to work, return false<br /> {<br /> // No point in drawing in device coordinates (pixmap will be scaled anyways).<br /> pixmap = sourcePixmap( Qt::LogicalCoordinates, &amp;offset; ); //, mode );<br /> }
<br /> QRectF bound = boundingRectFor( pixmap.rect() );
<br /> painter->save();<br /> painter->setPen( Qt::NoPen );<br /> painter->setBrush( mColor );<br /> QPointF p( offset.x()-mOffset.x(), offset.y()<s>mOffset.y() );<br /> bound.moveTopLeft( p );<br /> painter</s>>drawRoundedRect( bound, 5, 5, Qt::RelativeSize );<br /> painter->drawPixmap( offset, pixmap );<br /> painter->restore();<br />}
<br />//'''<br />
The strange thing in the implementation is that the sourceIsPixmap() function returns always false, even if the documentation states that "Returns true if the source effectively is a pixmap, e.g., a QGraphicsPixmapItem."
I ask to Qt gurus what can be going on here…
The default color is pale yellow, semi-transparent and the border size is set to 1.5. To add the effect simply add this:
<br /> Pixmap *item = new Pixmap(kineticPix);<br /> .<br /> .<br /> HighlightEffect *effect = new HighlightEffect();
<br /> item</s>>setGraphicsEffect( effect );<br />
Here the modified animated tiles example with the effect turned on:
Animated tiles example with QGraphicsPixmapItem highlighting
Hope someone found this useful.