Results 1 to 8 of 8

Thread: QImage setPixel does not set alpha value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QImage setPixel does not set alpha value

    Hi,

    I am trying to change the pixel value of image by using setPixel() method.
    It is changing the RGB values successfully but not changing the Alpha value.
    It always shows value 255.

    The code is like this

    QImage image = pixmap->tpImage();
    int width = image.width();
    int height = image.height();
    for( i = 0 ; i < height; i++)
    {
    for( j = 0; j < width; j++ )
    {
    image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
    }
    }

    Thanks in advance
    ~Sanjay
    Last edited by sanjayshelke; 19th February 2009 at 14:12. Reason: spelling error

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QImage setPixel does not set alpha value

    What's the image format (QImage::Format) of your image? Can it handle alpha values?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QImage setPixel does not set alpha value

    Quote Originally Posted by sanjayshelke View Post
    image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
    And maybe this should be
    Qt Code:
    1. image.setPixel(j,i, QColor(0,0,0,0).rgba()) ;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QImage setPixel does not set alpha value

    hi,

    Image format is QImage::Format_ARGB32 and i have tried with QColor(0,0,0,0).rgba() also.

    But no effect.

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage setPixel does not set alpha value

    try using:

    QRgb qRgba ( int r, int g, int b, int a ) instead of qcolor.rgba()

  6. #6
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QImage setPixel does not set alpha value

    Hi,

    I want to correct my problem statement.

    Thing is that whenever i am trying to change pixel value using setPixel(), it actually changes the pixel values.

    e.g When i do;
    image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
    this makes the pixel transparent.
    But after making it transparent if i ask to image what is rgba value at that position,
    it shows me 0,0,0,255.
    Actually it should give 0,0,0,0 rgba values.

    I have written the code like this:

    QImage image = pixmap->tpImage();
    int width = image.width();
    int height = image.height();
    for( i = 0 ; i < height; i++)
    {
    for( j = 0; j < width; j++ )
    {
    image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
    QColor clrAtPixel(imgPixmap.pixel(ptPos));
    int r,g,b,a;
    clrAtPixel.getRgb(&r,&g,&b,&a);
    qDebug()<<r<<" "<<g<<" "<<b<<" "<<a;
    }
    }

    I think setPixel is working fine and it is making the pixel transparent. ( I have seen that the image becomes transparent)
    But after making it transparent when i ask for rgb values it gives 0,0,0,255.
    The black color has same RGBA values as above.
    Now my image is transparent but still it gives 0,0,0,255 rgba values.

    Can anyone tell me whjat i m doing wrong.


    Regards,
    ~Sanjay

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QImage setPixel does not set alpha value

    Quote Originally Posted by sanjayshelke View Post
    image.setPixel(j,i, QColor(0,0,0,0).rgb()) ;
    QColor clrAtPixel(imgPixmap.pixel(ptPos));
    You change the pixel at image but request the color at imgPixmap! So of course you won't get the correct alpha value. (and what is ptPos???)

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage setPixel does not set alpha value

    Quote Originally Posted by Lykurg View Post
    And maybe this should be
    Qt Code:
    1. image.setPixel(j,i, QColor(0,0,0,0).rgba()) ;
    To copy to clipboard, switch view to plain text mode 
    Had you paid attention to this ? You are setting only rgb data, alpha is omitted hence !

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. [SOLVED] QImage setPixel alpha issue
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2008, 15:06
  3. Multiple QPainters on QImage
    By fire in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2008, 13:10
  4. OGLWidget + setPixel = slow
    By JimJey in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2008, 20:08
  5. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 12:40

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.