Results 1 to 7 of 7

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

  1. #1
    Join Date
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    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?
    Last edited by mireiner; 28th February 2017 at 18:34.

  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 fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    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.
    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
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    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?
    Last edited by mireiner; 1st March 2017 at 10:15.

  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 fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    As I said, See QImage Image Transformations section.
    http://doc.qt.io/qt-5/qimage.html#image-transformations
    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
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    Quote Originally Posted by Santosh Reddy View Post
    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(..)?
    Last edited by mireiner; 1st March 2017 at 13:00.

  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 fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    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.
    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
    Feb 2015
    Posts
    14
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to fit a QImage or SVG to the size of its dynamic QPainter graphics content?

    Quote Originally Posted by Santosh Reddy View Post
    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.

Similar Threads

  1. copy QPainter content to a QImage
    By danics in forum Qt Programming
    Replies: 11
    Last Post: 17th January 2013, 07:38
  2. Dynamic Scrollarea Content
    By rouge in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2011, 09:37
  3. Replies: 1
    Last Post: 24th January 2011, 14:57
  4. Replies: 0
    Last Post: 26th October 2010, 18:59
  5. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 08:25

Tags for this Thread

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.