
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.
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:
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;
};
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;
};
To copy to clipboard, switch view to plain text mode
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?
p.
fillRect(alpha.
rect(),
QColor(val, val, val
));
p.end();
px.setAlphaChannel(alpha);
return px;
}
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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks