A sample highlighting QGraphicsEffect: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
'''English''' | '''English''' | ||
| [[:A_sample_highlighting_QGraphicsEffect_Persian|فارسی]] | |||
[[Category:Learning]] | [[Category:Learning]] | ||
[[Category:HowTo]] | |||
While learning Qt, I wrote this sample highlighting graphics effect to be used with rectangular QGraphicsPixmapItem. | 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 | highlight.h | ||
Line 9: | Line 12: | ||
<code>//*'''''' | <code>//*'''''' | ||
#ifndef HIGHLIGHT_H | #ifndef HIGHLIGHT_H | ||
#define HIGHLIGHT_H | |||
#include | #include <QtGui> | ||
//*'''''' | //*'''''' | ||
class HighlightEffect : public QGraphicsEffect | class HighlightEffect : public QGraphicsEffect | ||
{ | |||
Q_OBJECT | |||
Q_PROPERTY( QColor color READ color WRITE setColor ) | |||
Q_PROPERTY( QPointF offset READ offset WRITE setOffset ) | |||
public: | |||
HighlightEffect( qreal offset = 1.5 ); | |||
virtual QRectF boundingRectFor( const QRectF &amp;sourceRect; ) const; | |||
QColor color() const { return mColor;} | |||
void setColor( QColor &amp;color; ) { mColor = color;} | |||
QPointF offset() const { return mOffset;} | |||
void setOffset( QPointF offset ) { mOffset = offset;} | |||
protected: | protected: | ||
virtual void draw( QPainter *painter ); // , QGraphicsEffectSource '''source ); | |||
private: | |||
QColor mColor; | |||
QPointF mOffset; | |||
}; | |||
//''' | |||
#endif // HIGHLIGHT_H | #endif // HIGHLIGHT_H | ||
//*'''''' | //*'''''' | ||
</code> | |||
highlight.cpp | highlight.cpp | ||
Line 28: | Line 50: | ||
<code>//*'''''' | <code>//*'''''' | ||
#include | #include "highlight.h" | ||
//*'''''' | //*'''''' | ||
HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(), | HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(), | ||
mColor( 255, 255, 0, 128 ), // yellow, semi-transparent | |||
mOffset( offset, offset ) | |||
{ | |||
} | |||
QRectF HighlightEffect::boundingRectFor( const QRectF &amp;sourceRect; ) const | |||
{ | |||
return sourceRect.adjusted( -mOffset.x(), -mOffset.y(), mOffset.x(), mOffset.y() ); | |||
} | |||
void HighlightEffect::draw( QPainter '''painter ) | |||
{ | |||
QPoint offset; | |||
QPixmap pixmap; | |||
// if ( sourceIsPixmap() ) // doesn't seems to work, return false | |||
{ | |||
// No point in drawing in device coordinates (pixmap will be scaled anyways). | |||
pixmap = sourcePixmap( Qt::LogicalCoordinates, &amp;offset; ); //, mode ); | |||
} | |||
QRectF bound = boundingRectFor( pixmap.rect() ); | |||
painter->save(); | |||
painter->setPen( Qt::NoPen ); | |||
painter->setBrush( mColor ); | |||
QPointF p( offset.x()-mOffset.x(), offset.y()-mOffset.y() ); | |||
bound.moveTopLeft( p ); | |||
painter->drawRoundedRect( bound, 5, 5, Qt::RelativeSize ); | |||
painter->drawPixmap( offset, pixmap ); | |||
painter->restore(); | |||
} | |||
//''' | |||
</code> | |||
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: | |||
<code> | |||
Pixmap *item = new Pixmap(kineticPix); | |||
. | |||
. | |||
HighlightEffect *effect = new HighlightEffect(); | |||
item->setGraphicsEffect( effect ); | |||
</code> | |||
Here the modified animated tiles example with the effect turned on: | Here the modified animated tiles example with the effect turned on: |
Revision as of 09:52, 25 February 2015
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
#define HIGHLIGHT_H
#include <QtGui>
//*''''''
class HighlightEffect : public QGraphicsEffect
{
Q_OBJECT
Q_PROPERTY( QColor color READ color WRITE setColor )
Q_PROPERTY( QPointF offset READ offset WRITE setOffset )
public:
HighlightEffect( qreal offset = 1.5 );
virtual QRectF boundingRectFor( const QRectF &amp;sourceRect; ) const;
QColor color() const { return mColor;}
void setColor( QColor &amp;color; ) { mColor = color;}
QPointF offset() const { return mOffset;}
void setOffset( QPointF offset ) { mOffset = offset;}
protected:
virtual void draw( QPainter *painter ); // , QGraphicsEffectSource '''source );
private:
QColor mColor;
QPointF mOffset;
};
//'''
#endif // HIGHLIGHT_H
//*''''''
highlight.cpp
//*''''''
#include "highlight.h"
//*''''''
HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(),
mColor( 255, 255, 0, 128 ), // yellow, semi-transparent
mOffset( offset, offset )
{
}
QRectF HighlightEffect::boundingRectFor( const QRectF &amp;sourceRect; ) const
{
return sourceRect.adjusted( -mOffset.x(), -mOffset.y(), mOffset.x(), mOffset.y() );
}
void HighlightEffect::draw( QPainter '''painter )
{
QPoint offset;
QPixmap pixmap;
// if ( sourceIsPixmap() ) // doesn't seems to work, return false
{
// No point in drawing in device coordinates (pixmap will be scaled anyways).
pixmap = sourcePixmap( Qt::LogicalCoordinates, &amp;offset; ); //, mode );
}
QRectF bound = boundingRectFor( pixmap.rect() );
painter->save();
painter->setPen( Qt::NoPen );
painter->setBrush( mColor );
QPointF p( offset.x()-mOffset.x(), offset.y()-mOffset.y() );
bound.moveTopLeft( p );
painter->drawRoundedRect( bound, 5, 5, Qt::RelativeSize );
painter->drawPixmap( offset, pixmap );
painter->restore();
}
//'''
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:
Pixmap *item = new Pixmap(kineticPix);
.
.
HighlightEffect *effect = new HighlightEffect();
item->setGraphicsEffect( effect );
Here the modified animated tiles example with the effect turned on:
Animated tiles example with QGraphicsPixmapItem highlighting
Hope someone found this useful.