PDA

View Full Version : QGraphicsPixmapItem - modifying the alpha channel of a QPixmap



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

wysota
3rd November 2006, 10:08
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.


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:


class Animator : public QObject {
Q_OBJECT
public:
Animator(MyClass *o){
object = o;
}
void start(int ms){
timer.start(ms);
}
void stop() { timer.stop(); }
protected:
QTimer timer;
MyClass *object;
protected slots:
void on_timer_timeout(){
object->fadeMeMore();
}
};

class MyClass ... {
public:
MyClass(...){
anim = new Animator(this);
}
...
protected:
Animator *anim;
};


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?


QPixmap &setAlpha(QPixmap &px, int val){
QPixmap alpha = px;
QPainter p(&alpha);
p.fillRect(alpha.rect(), QColor(val, val, val));
p.end();
px.setAlphaChannel(alpha);
return px;
}

Claymore
3rd November 2006, 22:20
BTW. Your method for updating alpha is very slow. How about this?

QPixmap &setAlpha(QPixmap &px, int val)
{
QPixmap alpha = px;
QPainter p(&alpha);
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! :)