#ifndef PIXMAPITEM_H
#define PIXMAPITEM_H
#include <QtGui>
{
public:
PixmapItem
(const QPixmap
& pixmap,
QGraphicsScene* scene
=0, qreal x
=0, qreal y
=0): m_alpha(0),
m_pos(x,y)
{
// Start with the image completely transparent
UpdateAlphaChannel(m_alpha);
setPos(m_pos);
// Add the item to the scene
if(NULL != scene)
scene->addItem(this);
}
protected:
{
// If the alpha level is 255, then kill the timer
if(m_alpha >= 255)
{
killTimer(event->timerId());
return;
}
UpdateAlphaChannel(m_alpha++);
}
private:
int m_alpha; // Current alpha level
QPointF m_pos;
// Current position in the scene
void UpdateAlphaChannel(int alpha)
{
QImage image
(pixmap
().
toImage());
for(int x = 0; x < image.width(); x++)
{
for(int y = 0; y < image.height(); y++)
{
QColor color
(image.
pixel(x,y
));
color.setAlpha(alpha);
image.setPixel(x,y,color.rgba());
}
}
setPixmap
(QPixmap::fromImage(image
));
}
};
#endif // PIXMAPITEM_H
#ifndef PIXMAPITEM_H
#define PIXMAPITEM_H
#include <QtGui>
class PixmapItem : public QObject, public QGraphicsPixmapItem
{
public:
PixmapItem(const QPixmap& pixmap, QGraphicsScene* scene=0, qreal x=0, qreal y=0):
QGraphicsPixmapItem(pixmap, 0, scene),
m_alpha(0),
m_pos(x,y)
{
// Start with the image completely transparent
UpdateAlphaChannel(m_alpha);
setPos(m_pos);
// Add the item to the scene
if(NULL != scene)
scene->addItem(this);
}
protected:
void timerEvent(QTimerEvent* event)
{
// If the alpha level is 255, then kill the timer
if(m_alpha >= 255)
{
killTimer(event->timerId());
return;
}
UpdateAlphaChannel(m_alpha++);
}
private:
int m_alpha; // Current alpha level
QPointF m_pos; // Current position in the scene
void UpdateAlphaChannel(int alpha)
{
QImage image(pixmap().toImage());
for(int x = 0; x < image.width(); x++)
{
for(int y = 0; y < image.height(); y++)
{
QColor color(image.pixel(x,y));
color.setAlpha(alpha);
image.setPixel(x,y,color.rgba());
}
}
setPixmap(QPixmap::fromImage(image));
}
};
#endif // PIXMAPITEM_H
To copy to clipboard, switch view to plain text mode
Bookmarks