A sample highlighting QGraphicsEffect

http://qt-project.org/wiki/Category:HowTo

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 &sourceRect; ) const;
 QColor          color() const { return mColor;}
 void            setColor( QColor &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;
};

 HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(),
   mColor( 255, 255, 0, 128 ),  // yellow, semi-transparent
   mOffset( offset, offset )
{
}
 
QRectF HighlightEffect::boundingRectFor( const QRectF &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, &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();
}

        Pixmap *item = new Pixmap(kineticPix);
        .
        .
        HighlightEffect *effect = new HighlightEffect();
 
        item->setGraphicsEffect( effect );


暂时做记录,是个很好的高光效果.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值