PDA

View Full Version : How to fit a QImage or SVG to the size of its dynamic QPainter graphics content?



mireiner
28th February 2017, 18:28
Hi there,

Using Qt 5.8 QPainter graphics (only text, lines and ellipses) are drawn and saved to QImage (PNG) and QSvgGenerator (SVG) files.
The graphic is dynamic in size. Because of that the graphic is drawn in an QImage or SVG with maximum size.
But the QImage / SVG should fit exactly to the size of the graphic. How can this be done?

1. My thoughts were, there might be a Qt method that can detect the size of the bounding rectangle of the QPainter graphic in the QImage. So the graphic can be drawn again in an QImage with the size of the detected rectangle. So graphic and image fit exactly.

2. Or there might be a Qt function that is able to cut an QImage to the size of its graphic content automatically. Is there such?

But maybe I think more complicated as it is. What is the best solution for this problem?

Santosh Reddy
1st March 2017, 05:49
Both solutions are normally used, it depends on your environment and needs.

1. With bonding rect information, you will have to paint the image every time before display. (this is how QWidgets are painted normally). See QImage rect()

2. With painting already done, you will have to apply transformation on the image. (this is also a common practice), See QImage Image Transformations section.

mireiner
1st March 2017, 10:10
Sorry, because of my poor English I probably wasn't clear enough in my first post.

A QPainter graphic should be drawn into a QImage. And this QImage is saved to a file (not drawn on screen).

When the QPainter graphic is drawn into the QImage the size of the graphic isn't known before. The graphic size can vary a third of its size. But the graphic resolution should stay always fixed! So scaling the graphic is no option. But the size of the QImage should be sized / resize to the size of the QPainter graphic!

Now I first create a QImage with the maximal size the QPainter graphic can reach. And then the QPainter graphic is drawn into this QImage. But in this way most of the time the QImage is much larger than its graphic content. Because the graphic always begins in the upper left corner the blank white parts of the QImage are on its right and lower side.

The question is how to shrink the QImage to the size of its fixed size graphic content? Or how to cut the blank parts of the QImage so the QImage got exact the size of its graphic content?

Santosh Reddy
1st March 2017, 12:14
As I said, See QImage Image Transformations section.
http://doc.qt.io/qt-5/qimage.html#image-transformations

mireiner
1st March 2017, 12:53
As I said, See QImage Image Transformations section.
http://doc.qt.io/qt-5/qimage.html#image-transformations

Yes a QImage can be scaled with its method QImage::scaled(const QSize &size...). But the problem is that I don't know the exact size of the graphics content. So I don't know to what size I have to scale the QImage.

Does Qt provide any method that can detect the size of a graphic that was drawn with QPainter? Or detect the size of the graphic content in an QImage (The empty blank space in the QImage is always color white).

For example a method like QImage::resizeToContent(..) or QPainter::getGraphicSize(..)?

Santosh Reddy
1st March 2017, 13:04
I think (if not mistaken) now I got your problem...

No you cannot detect the size of the QImage painted by QPainter, it works other way, you are supported to set the size of QImage and QPainter will respect it.

Looks like you need to use QGraphicsView framework to draw the items and then calculated the bonding box and then generate the QImage/SVG from there.

mireiner
1st March 2017, 16:01
Looks like you need to use QGraphicsView framework to draw the items and then calculated the bonding box and then generate the QImage/SVG from there.

Ok, then I will have to look into QGraphicsView framework. Thank for answering and your patience.