QGraphicsPixmapItem - modifying the alpha channel of a QPixmap
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:
Code:
#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
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
Re: QGraphicsPixmapItem - modifying the alpha channel of a QPixmap
Quote:
Originally Posted by
Claymore
Hello! I posted this thread over at QtForum.org, but figured I'd post it over here too - I hope yall don't mind :).
We don't mind.
Quote:
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).
You don't need to inherit QObject for that. Trolltech usually creates something like an "animator" object (QObject subclass) that performs the animation on the object (you don't get the QObject legacy which has its advantages - you can even use a single animator object for all items). Something like this:
Code:
Q_OBJECT
public:
Animator(MyClass *o){
object = o;
}
void start(int ms){
timer.start(ms);
}
void stop() { timer.stop(); }
protected:
MyClass *object;
protected slots:
void on_timer_timeout(){
object->fadeMeMore();
}
};
class MyClass ... {
public:
MyClass(...){
anim = new Animator(this);
}
...
protected:
Animator *anim;
};
Quote:
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? :)
What happens if you just use a QGraphicsPixmapItem with a pixmap that has some alpha channel? Does the alpha get displayed correctly? Maybe it's a limitation of QGraphicsPixmapItem and you need to do the drawing yourself by setting a composition mode or something like that?
BTW. Your method for updating alpha is very slow. How about this?
Code:
p.
fillRect(alpha.
rect(),
QColor(val, val, val
));
p.end();
px.setAlphaChannel(alpha);
return px;
}
Re: QGraphicsPixmapItem - modifying the alpha channel of a QPixmap
Quote:
BTW. Your method for updating alpha is very slow. How about this?
Code:
{
p.
fillRect(alpha.
rect(),
QColor(val, val, val
));
p.end();
px.setAlphaChannel(alpha);
return px;
}
That method worked beautifully! I didn't even think of using QPainter to handle the painting... *bonks self* Thanks a bunch! :)