Results 1 to 3 of 3

Thread: QGraphicsPixmapItem - modifying the alpha channel of a QPixmap

  1. #1
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. #ifndef PIXMAPITEM_H
    2. #define PIXMAPITEM_H
    3.  
    4. #include <QtGui>
    5.  
    6. class PixmapItem : public QObject, public QGraphicsPixmapItem
    7. {
    8. public:
    9. PixmapItem(const QPixmap& pixmap, QGraphicsScene* scene=0, qreal x=0, qreal y=0):
    10. QGraphicsPixmapItem(pixmap, 0, scene),
    11. m_alpha(0),
    12. m_pos(x,y)
    13. {
    14. // Start with the image completely transparent
    15. UpdateAlphaChannel(m_alpha);
    16. setPos(m_pos);
    17.  
    18. // Add the item to the scene
    19. if(NULL != scene)
    20. scene->addItem(this);
    21. }
    22.  
    23. protected:
    24. void timerEvent(QTimerEvent* event)
    25. {
    26. // If the alpha level is 255, then kill the timer
    27. if(m_alpha >= 255)
    28. {
    29. killTimer(event->timerId());
    30. return;
    31. }
    32.  
    33. UpdateAlphaChannel(m_alpha++);
    34. }
    35.  
    36. private:
    37. int m_alpha; // Current alpha level
    38. QPointF m_pos; // Current position in the scene
    39.  
    40. void UpdateAlphaChannel(int alpha)
    41. {
    42. QImage image(pixmap().toImage());
    43. for(int x = 0; x < image.width(); x++)
    44. {
    45. for(int y = 0; y < image.height(); y++)
    46. {
    47. QColor color(image.pixel(x,y));
    48. color.setAlpha(alpha);
    49. image.setPixel(x,y,color.rgba());
    50. }
    51. }
    52. setPixmap(QPixmap::fromImage(image));
    53. }
    54. };
    55.  
    56. #endif // PIXMAPITEM_H
    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?

    Thanks!
    - Clay
    Last edited by Claymore; 31st October 2006 at 16:36.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsPixmapItem - modifying the alpha channel of a QPixmap

    Quote Originally Posted by Claymore View Post
    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:
    Qt Code:
    1. class Animator : public QObject {
    2. Q_OBJECT
    3. public:
    4. Animator(MyClass *o){
    5. object = o;
    6. }
    7. void start(int ms){
    8. timer.start(ms);
    9. }
    10. void stop() { timer.stop(); }
    11. protected:
    12. QTimer timer;
    13. MyClass *object;
    14. protected slots:
    15. void on_timer_timeout(){
    16. object->fadeMeMore();
    17. }
    18. };
    19.  
    20. class MyClass ... {
    21. public:
    22. MyClass(...){
    23. anim = new Animator(this);
    24. }
    25. ...
    26. protected:
    27. Animator *anim;
    28. };
    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?

    Qt Code:
    1. QPixmap &setAlpha(QPixmap &px, int val){
    2. QPixmap alpha = px;
    3. QPainter p(&alpha);
    4. p.fillRect(alpha.rect(), QColor(val, val, val));
    5. p.end();
    6. px.setAlphaChannel(alpha);
    7. return px;
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 2 users say thank you to wysota for this useful post:

    Claymore (3rd November 2006), sunil.thaha (13th November 2006)

  4. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QGraphicsPixmapItem - modifying the alpha channel of a QPixmap

    BTW. Your method for updating alpha is very slow. How about this?
    Qt Code:
    1. QPixmap &setAlpha(QPixmap &px, int val)
    2. {
    3. QPixmap alpha = px;
    4. QPainter p(&alpha);
    5. p.fillRect(alpha.rect(), QColor(val, val, val));
    6. p.end();
    7. px.setAlphaChannel(alpha);
    8. return px;
    9. }
    To copy to clipboard, switch view to plain text mode 
    That method worked beautifully! I didn't even think of using QPainter to handle the painting... *bonks self* Thanks a bunch!

Similar Threads

  1. Alpha channel weirdness with QGLContext
    By renaissanz in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2006, 16:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.