A sample highlighting QGraphicsEffect: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(convert {{doclinkanchor}} to the improved {{doclink}})
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''English'''<br />| [[:A_sample_highlighting_QGraphicsEffect_Persian|فارسی]]
[[Category:HowTo]]
__NOEDITSECTION__
__NOTOC__
== Introduction ==


[[Category:Learning]]<br />[[Category:HowTo]]
While learning Qt, I wrote this sample highlighting graphics effect to be used with rectangular {{DocLink|QGraphicsPixmapItem}}... and I use this post just to learn how to publish a wiki page here too.. ;-)


While learning Qt, I wrote this sample highlighting graphics effect to be used with rectangular QGraphicsPixmapItem.<br />..and I use this post just to learn how to publish a wiki page here too.. ;-)
'''highlight.h'''


highlight.h
<code>
#ifndef HIGHLIGHT_H
#define HIGHLIGHT_H


<code>//*''''''
#include <QtGui>


#ifndef HIGHLIGHT_H<br />#define HIGHLIGHT_H
class HighlightEffect : public QGraphicsEffect
{
Q_OBJECT
Q_PROPERTY( QColor color READ color WRITE setColor )
Q_PROPERTY( QPointF offset READ offset WRITE setOffset )


#include &lt;QtGui&amp;gt;
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 );


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;amp;sourceRect; ) const;<br /> QColor color() const { return mColor;}<br /> void setColor( QColor &amp;amp;color; ) { mColor = color;}<br /> QPointF offset() const { return mOffset;}<br /> void setOffset( QPointF offset ) { mOffset = offset;}
private:
  QColor mColor;
  QPointF mOffset;
};


protected:<br /> virtual void draw( QPainter *painter ); // , QGraphicsEffectSource '''source );<br />private:<br /> QColor mColor;<br /> QPointF mOffset;<br />};
#endif // HIGHLIGHT_H
<br />//'''
</code>
 
'''highlight.cpp'''
 
<code>
#include "highlight.h"


#endif // HIGHLIGHT_H
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;


//*''''''<br /></code>
// 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; );
//}


highlight.cpp
QRectF bound = boundingRectFor( pixmap.rect() );


<code>//*''''''
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>


#include &quot;highlight.h&amp;quot;
The strange thing in the implementation is that the {{DocLink|QGraphicsEffect|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 asked the 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:


HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(),<br /> mColor( 255, 255, 0, 128 ), // yellow, semi-transparent<br /> mOffset( offset, offset )<br />{<br />}
<code>
Pixmap *item = new Pixmap(kineticPix);
.
.
HighlightEffect *effect = new HighlightEffect();


QRectF HighlightEffect::boundingRectFor( const QRectF &amp;amp;sourceRect; ) const<br />{<br /> return sourceRect.adjusted( -mOffset.x(), <s>mOffset.y(), mOffset.x(), mOffset.y() );<br />}
item->setGraphicsEffect( effect );
<br />void HighlightEffect::draw( QPainter '''painter )<br />{<br /> QPoint offset;<br /> QPixmap pixmap;
</code>
<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;amp;offset; ); //, mode );<br /> }
<br /> QRectF bound = boundingRectFor( pixmap.rect() );
<br /> painter-&gt;save();<br /> painter-&gt;setPen( Qt::NoPen );<br /> painter-&gt;setBrush( mColor );<br /> QPointF p( offset.x()-mOffset.x(), offset.y()<s>mOffset.y() );<br /> bound.moveTopLeft( p );<br /> painter</s>&gt;drawRoundedRect( bound, 5, 5, Qt::RelativeSize );<br /> painter-&gt;drawPixmap( offset, pixmap );<br /> painter-&gt;restore();<br />}
<br />//'''<br /></code>
<br />The strange thing in the implementation is that the sourceIsPixmap() function returns always false, even if the documentation states that &quot;Returns true if the source effectively is a pixmap, e.g., a QGraphicsPixmapItem.&quot;<br />I ask to Qt gurus what can be going on here…
<br />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 /><code><br /> Pixmap *item = new Pixmap(kineticPix);<br /> .<br /> .<br /> HighlightEffect *effect = new HighlightEffect();
<br /> item</s>&gt;setGraphicsEffect( effect );<br /></code>


Here the modified animated tiles example with the effect turned on:
Here the modified animated tiles example with the effect turned on:


[[Image:http://lh3.ggpht.com/_WpmQ-TG8HcM/S-knW64KOJI/AAAAAAAABd4/m4YG0YXW_RU/s640/highlight.png|Animated tiles example with QGraphicsPixmapItem highlighting]]
[[File:Highlight.png]]
 
Animated tiles example with QGraphicsPixmapItem highlighting


Hope someone found this useful.
Hope someone finds this useful.

Latest revision as of 12:38, 21 October 2015


Introduction

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& 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;
};

#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& 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; );
 //} 

 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 asked the 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:

Highlight.png

Animated tiles example with QGraphicsPixmapItem highlighting

Hope someone finds this useful.