Claymore
31st October 2006, 16:22
Hello! I posted this thread over at QtForum.org, but figured I'd post it over here too - I hope yall don't mind :).
Basically what I'm trying to do is create a subclass that inherits both the QGraphicsPixmapItem and QObject classes. I'm inheriting the QObject class so I can setup a timer that gradually fades a QPixmap into view (by slowly incrementing the alpha channel's value on each pixel of the image).
Here is the class I've written that handles this:
#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
Currently, the function UpdateAlphaChannel() is correctly setting the alpha channel of the image to the specified value, but the image does not get displayed with the correct alpha value. I've stepped through the code and made sure that the colors are being set properly, and they are. I've also double-checked that the image I'm using has an alpha channel by calling the functions hasAlpha() and hasAlphaChannel() on the QPixmap being used, and both return true.
So my question is what am I doing wrong? :)
Thanks!
- Clay
Basically what I'm trying to do is create a subclass that inherits both the QGraphicsPixmapItem and QObject classes. I'm inheriting the QObject class so I can setup a timer that gradually fades a QPixmap into view (by slowly incrementing the alpha channel's value on each pixel of the image).
Here is the class I've written that handles this:
#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
Currently, the function UpdateAlphaChannel() is correctly setting the alpha channel of the image to the specified value, but the image does not get displayed with the correct alpha value. I've stepped through the code and made sure that the colors are being set properly, and they are. I've also double-checked that the image I'm using has an alpha channel by calling the functions hasAlpha() and hasAlphaChannel() on the QPixmap being used, and both return true.
So my question is what am I doing wrong? :)
Thanks!
- Clay