Search code examples
qt5image-resizingqimageqtextdocument

width/height ratio preserved after a call to qtextimageformat.setWidth()?


In order to insert an image in a QTextDocument I have to modify its width and its height and to keep the original image's ratio. Hence my code :

qtextimageformat.setWidth( new_width );
cursor.insertImage( qtextimageformat, position )

I thought it was mandatory to modify the width and height but I was surprized to see that the call to .setWidth() somehow preserved the image's ratio, as if .setHeight() was implicitly called with the right parameter.

Is this the expected behaviour ? Or I am missing something ? The documentation didn't help me.


Solution

  • This is expected behavior, even if it is not explicit in documentation. The static functions QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format) and QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format) in qtextimagehandler.cpp automatically deduce the height if only width was specified in QTextImageFormat (and the opposite too)