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.
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.
Background being transparent or opaque does not change the way to use QPainter. Draw the same way as if the background was opaque.I want to know the usual way to paint an image with transparent background by using QPainter.
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.
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:
But, while resizing the widget, the image shown is muddledness.Qt Code:
{ QPainter p; p.begin( &a ); p.drawLine(...); p.end(); p.begin( this ); p.drawImage( 0, 0, a ); p.end(); }To copy to clipboard, switch view to plain text mode
So what shall I do?
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 10: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.
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.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?If want to store the images as cache then QPixmap will be better option.Originally Posted by Qt Docs
Last edited by Santosh Reddy; 10th December 2013 at 10: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.
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.
Also need to fill the QPixmap's background with transparent color firstly?If want to store the images as cache then QPixmap will be better option.
Sounds fair, give it a try (If you are interesrted I will recommend to measure the performance improvement because of cache)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.
Yes.Also need to fill the QPixmap's background with transparent color firstly?
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.
Bookmarks