Results 1 to 3 of 3

Thread: QGraphicsPixmapItem - modifying the alpha channel of a QPixmap

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.