Results 1 to 9 of 9

Thread: How to paint an image with transparent background?

  1. #1
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default How to paint an image with transparent background?

    Hi,
    I am a newbie for QT.And I want to know the usual way to paint an image with transparent background by using QPainter.
    eg. to paint a line on the transparent background.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to paint an image with transparent background?

    I want to know the usual way to paint an image with transparent background by using QPainter.
    Background being transparent or opaque does not change the way to use QPainter. Draw the same way as if the background was opaque.

    Refer DrawLine
    Refer Basic Drawing Example
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to paint an image with transparent background?

    Santosh Reddy,
    Thank you for your reply.
    I am not sure whether the way I want to implement my application is reasonable. Here I will try to explain it.Hope your advice.
    I will build a viewlayer tree.For each viewlayer in the tree, I set a cache which is a QImage that I will draw all the items of the current viewlayer on.
    Then at last, I render the images one by one on the root viewlayer's cache image which will be painted on the widget finally.
    Based on that thought, I need to draw each image of the viewlayer as transparent.
    But before I have made a test by creating a QImage and draw a line on it, like below:
    Qt Code:
    1. void MyWidget::paintEvent ( QPaintEvent * event )
    2. {
    3. QImage a( width(), height(), QImage::Format_ARGB32_Premultiplied );
    4. p.begin( &a );
    5. p.drawLine(...);
    6. p.end();
    7. p.begin( this );
    8. p.drawImage( 0, 0, a );
    9. p.end();
    10. }
    To copy to clipboard, switch view to plain text mode 
    But, while resizing the widget, the image shown is muddledness.
    So what shall I do?

    Quote Originally Posted by Santosh Reddy View Post
    Background being transparent or opaque does not change the way to use QPainter. Draw the same way as if the background was opaque.

    Refer DrawLine
    Refer Basic Drawing Example

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to paint an image with transparent background?

    I don't think QImage cache will help you, draw directly on the widget instead.

    Anyway if you want to go with QImage, then first fill the QImage background with transparent color, (a.fill(Qt::transparent);)
    Last edited by Santosh Reddy; 10th December 2013 at 11:24. Reason: Disabled smilies in text
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to paint an image with transparent background?

    Will there be a performance issue when to draw directly on the widget?
    Because during each repainting procedure, all the things need to be re-calculated and re-drawn.

    QPixmap/QBitmap/QImage which one will be better, or they are the same in my situation?


    Quote Originally Posted by Santosh Reddy View Post
    I don't think QImage cache will help you, draw directly on the widget instead.

    Anyway if you want to go with QImage, then first fill the QImage background with transparent color, (a.fill(Qt::transparent)

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to paint an image with transparent background?

    Will there be a performance issue when to draw directly on the widget?
    Because during each repainting procedure, all the things need to be re-calculated and re-drawn.
    Do all the needed calculations in other than paintEvent() function, and paint (repaint) in the paintEvent() function. (Note there will no real performance improvement un-less calculations are done in a worker thread, but wait mostly calculations when wisely done can be included in paintEvent() it self.) Again it depends on how CPU intensive are the calculations.

    QPixmap/QBitmap/QImage which one will be better, or they are the same in my situation?
    Quote Originally Posted by Qt Docs
    Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.
    If want to store the images as cache then QPixmap will be better option.
    Last edited by Santosh Reddy; 10th December 2013 at 11:18.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to paint an image with transparent background?

    Quote Originally Posted by Santosh Reddy View Post
    Do all the needed calculations in other than paintEvent() function, and paint (repaint) in the paintEvent() function.
    Yes, what I want to do with the cache is to gain that goal.So I want to draw all the things on the image before, and if nothing changed, but the paintEvent() is called , I just draw the image again.
    And also, if only some viewlayer is changed, it's not needed to re-calculate the items on the other viewlayers, so I want the image cache.

    If want to store the images as cache then QPixmap will be better option.
    Also need to fill the QPixmap's background with transparent color firstly?

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to paint an image with transparent background?

    Yes, what I want to do with the cache is to gain that goal.So I want to draw all the things on the image before, and if nothing changed, but the paintEvent() is called , I just draw the image again.
    And also, if only some viewlayer is changed, it's not needed to re-calculate the items on the other viewlayers, so I want the image cache.
    Sounds fair, give it a try (If you are interesrted I will recommend to measure the performance improvement because of cache)

    Also need to fill the QPixmap's background with transparent color firstly?
    Yes.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Jun 2013
    Posts
    56
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to paint an image with transparent background?

    Quote Originally Posted by Santosh Reddy View Post
    Sounds fair, give it a try (If you are interesrted I will recommend to measure the performance improvement because of cache)


    Yes.
    But as you tell me the initialization is required, I have no enough confidence on that. I will try.

Similar Threads

  1. Transparent Push button over background image
    By KumarKandasamy in forum Newbie
    Replies: 2
    Last Post: 15th May 2014, 08:22
  2. Replies: 4
    Last Post: 27th November 2013, 16:15
  3. Replies: 3
    Last Post: 13th November 2011, 09:12
  4. Replies: 1
    Last Post: 25th June 2010, 19:31
  5. Transparent background on QLabel on transparent QWidget
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2008, 03: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.