Difference between revisions of "A sample highlighting QGraphicsEffect"

From Qt Wiki
Jump to: navigation, search
(Corrected Layout after Wiki Conversion)
Line 1: Line 1:
'''English'''
 
| [[:A_sample_highlighting_QGraphicsEffect_Persian|فارسی]]
 
 
[[Category:Learning]]
 
 
[[Category:HowTo]]
 
[[Category:HowTo]]
 +
__NOEDITSECTION__
 +
__NOTOC__
  
While learning Qt, I wrote this sample highlighting graphics effect to be used with rectangular QGraphicsPixmapItem.
+
== Introduction ==
..and I use this post just to learn how to publish a wiki page here too.. ;-)
 
  
highlight.h
+
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.. ;-)
  
<code>//*''''''
+
'''highlight.h'''
  
 +
<code>
 
#ifndef HIGHLIGHT_H
 
#ifndef HIGHLIGHT_H
 
#define HIGHLIGHT_H
 
#define HIGHLIGHT_H
  
 
#include <QtGui>
 
#include <QtGui>
 
//*''''''
 
  
 
class HighlightEffect : public QGraphicsEffect
 
class HighlightEffect : public QGraphicsEffect
Line 24: Line 20:
 
  Q_PROPERTY( QColor color READ color WRITE setColor )
 
  Q_PROPERTY( QColor color READ color WRITE setColor )
 
  Q_PROPERTY( QPointF offset READ offset WRITE setOffset )
 
  Q_PROPERTY( QPointF offset READ offset WRITE setOffset )
public:
 
HighlightEffect( qreal offset = 1.5 );
 
virtual QRectF boundingRectFor( const QRectF &amp;amp;sourceRect; ) const;
 
QColor color() const { return mColor;}
 
void setColor( QColor &amp;amp;color; ) { mColor = color;}
 
QPointF offset() const { return mOffset;}
 
void setOffset( QPointF offset ) { mOffset = offset;}
 
  
protected:
+
public:
virtual void draw( QPainter *painter ); // , QGraphicsEffectSource '''source );
+
  HighlightEffect( qreal offset = 1.5 );
private:
+
  virtual QRectF boundingRectFor( const QRectF& sourceRect; ) const;
QColor mColor;
+
  QColor color() const { return mColor;}
QPointF mOffset;
+
  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
 
#endif // HIGHLIGHT_H
 
//*''''''
 
 
</code>
 
</code>
  
highlight.cpp
+
'''highlight.cpp'''
 
 
<code>//*''''''
 
  
 +
<code>
 
#include "highlight.h"
 
#include "highlight.h"
  
//*''''''
+
HighlightEffect::HighlightEffect( qreal offset ) :
 
+
QGraphicsEffect(),
HighlightEffect::HighlightEffect( qreal offset ) : QGraphicsEffect(),
 
 
  mColor( 255, 255, 0, 128 ), // yellow, semi-transparent
 
  mColor( 255, 255, 0, 128 ), // yellow, semi-transparent
  mOffset( offset, offset )
+
  mOffset( offset, offset ) {}
{
 
}
 
  
QRectF HighlightEffect::boundingRectFor( const QRectF &amp;amp;sourceRect; ) const
+
QRectF HighlightEffect::boundingRectFor( const QRectF& sourceRect; ) const
 
{
 
{
 
  return sourceRect.adjusted( -mOffset.x(), -mOffset.y(), mOffset.x(), mOffset.y() );
 
  return sourceRect.adjusted( -mOffset.x(), -mOffset.y(), mOffset.x(), mOffset.y() );
 
}
 
}
  
void HighlightEffect::draw( QPainter '''painter )
+
void HighlightEffect::draw( QPainter* painter )
 
{
 
{
 
  QPoint offset;
 
  QPoint offset;
 
  QPixmap pixmap;
 
  QPixmap pixmap;
  
// if ( sourceIsPixmap() ) // doesn't seems to work, return false
+
// if ( sourceIsPixmap() ) // doesn't seems to work, return false
  {
+
  // {
 
  // No point in drawing in device coordinates (pixmap will be scaled anyways).
 
  // No point in drawing in device coordinates (pixmap will be scaled anyways).
  pixmap = sourcePixmap( Qt::LogicalCoordinates, &amp;amp;offset; ); //, mode );
+
  pixmap = sourcePixmap( Qt::LogicalCoordinates, &offset; );
}  
+
//}  
  
 
  QRectF bound = boundingRectFor( pixmap.rect() );
 
  QRectF bound = boundingRectFor( pixmap.rect() );
Line 87: Line 77:
 
  painter->restore();
 
  painter->restore();
 
}
 
}
 
//'''
 
 
</code>
 
</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."
+
The strange thing in the implementation is that the {{DocLinkAnchor|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 ask to Qt gurus what can be going on here…
+
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:
 
The default color is pale yellow, semi-transparent and the border size is set to 1.5. To add the effect simply add this:
Line 107: Line 95:
 
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.

Revision as of 22:46, 3 March 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

  1. ifndef HIGHLIGHT_H
  2. define HIGHLIGHT_H
  1. 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;

};

  1. endif // HIGHLIGHT_H

highlight.cpp

  1. 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.