Results 1 to 12 of 12

Thread: QImage and Qt::transparent

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QImage and Qt::transparent

    hi
    i want to paint on an image.How to make the image background as transparent.

    the following code can change the background color of the image .

    Qt Code:
    1. QColormap colormap=QColormap::instance();
    2. image.fill(colormap.pixel(Qt::green));
    To copy to clipboard, switch view to plain text mode 

    but it fails to make the background transparent even when the code is changed to
    Qt Code:
    1. QColormap colormap=QColormap::instance();
    2. image.fill(colormap.pixel(Qt::transparent));
    To copy to clipboard, switch view to plain text mode 

    i wnt to paint on aQImage rather than QPixmap is because, i am painting in a multithreaded program.

    how to make the background of a QImage as transparent.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage and Qt::transparent

    try:
    Qt Code:
    1. image.fill(qRgba(0, 0, 0, 255));
    To copy to clipboard, switch view to plain text mode 

    and make sure the QImage depth is 32 bits.

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QImage and Qt::transparent

    The image depth is 32 but had not worked.

    Qt Code:
    1. image = QImage(1000,1000, QImage::Format_RGB32);
    2. qDebug()<<"depth "<<image.depth(); //prints 32
    3. image.fill(qRgba(0, 0, 0, 255));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QImage and Qt::transparent

    how do you test if the image is transparent or not?

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QImage and Qt::transparent

    after the above code

    //code in worker thread
    Qt Code:
    1. QPainter painter(&image);
    2. painter.save();
    3. //painting is performed
    4. painter.restore();
    To copy to clipboard, switch view to plain text mode 


    //code in main gui thread.
    Qt Code:
    1. pixmap=QPixmap::fromImage(worker_thread.image);//image is converted to pixmap
    2. update();//inside the paintevent the pixmap is painted on to widget.
    To copy to clipboard, switch view to plain text mode 

    i get black as the background color.

    But if any color is given(instead of Qt::transparent) such as green then green color is the background color
    Qt Code:
    1. image.fill(colormap.pixel(Qt::green));
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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 and Qt::transparent

    what are u trying to achieve ? I am still not clear abt it .

    Are u making an application by which user can draw over a area, with an image in the bcground ??
    Sort of image editing application ? am i getting u right ?

  7. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QImage and Qt::transparent

    Sort of image editing application ?
    no

    A image is constructed(made) by painting on a QImage object with QPainter .Before painting on the image the image background should be set to transparent.

  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 and Qt::transparent

    ohh oh !!!
    I guess i caught the mistake...
    image.fill(qRgba(0, 0, 0, 255)); should be
    image.fill(qRgba(0, 0, 0, 0));
    255 specifies NO OPACITY !!

    Hope this will solve the prob

  9. #9
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QImage and Qt::transparent

    thanks for reply

    but it did not work

  10. #10
    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: QImage and Qt::transparent

    QImage::Format_RGB32 doesn't support transparency. Use QImage::Format_ARGB32

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

    babu198649 (16th April 2008), Cupidvogel (13th February 2016)

  12. #11
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QImage and Qt::transparent

    thanks wysota
    constructing the image with QImage::Format_ARGB32 and then using image.fill(qRgba(0,0,0,0));
    makes the image transparent

  13. #12
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QImage and Qt::transparent

    Hi, sorry to bump this thread, but is it possible to make a QIcon out of an image where some parts are transparent, and then fill the opaque parts of the icon with some background color?

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.