{"id":24,"date":"2010-04-11T13:19:51","date_gmt":"2010-04-11T13:19:51","guid":{"rendered":"http:\/\/simion314.wordpress.com\/?p=24"},"modified":"2010-04-11T13:19:51","modified_gmt":"2010-04-11T13:19:51","slug":"creating-a-imagepicture-widget-in-qt4-that-keeps-the-aspect-ration-has-zoom-and-rotate-features","status":"publish","type":"post","link":"http:\/\/ploscariu.com\/wordpress\/2010\/04\/11\/creating-a-imagepicture-widget-in-qt4-that-keeps-the-aspect-ration-has-zoom-and-rotate-features\/","title":{"rendered":"Creating a Image,Picture widget in Qt4 that keeps the aspect ration has zoom and rotate features"},"content":{"rendered":"<p>To display image in Qt4 you can use a QLabel and the setPixmap() method. But if you need something more complex that will keep the aspect ratio and can zoom and rotate you will have to create a custom widget. This is how i did it for a Image viewer application that i make it in Qt4 using c++.<\/p>\n<p><code>#ifndef IMAGEWIDGET_H<\/code><\/p>\n<p><code>#define IMAGEWIDGET_H<\/code><\/p>\n<p><code>#include &lt;QWidget&gt;<\/code><\/p>\n<p><code>class QSize;<\/code><\/p>\n<p><code>class QImage;<\/code><\/p>\n<p><code>class ImageWidget : public QWidget<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code> Q_OBJECT<\/code><\/p>\n<p><code>public:<\/code><\/p>\n<p><code> explicit ImageWidget(QWidget *parent = 0);<\/code><\/p>\n<p><code> void setImage(const QImage &amp;image);<\/code><\/p>\n<p><code> QImage *getImage();<\/code><\/p>\n<p><code> float getZoomFactor();<\/code><\/p>\n<p><code> \/\/this is absolute depends only on the size of the image<\/code><\/p>\n<p><code> void setZoomFactor(float zf);<\/code><\/p>\n<p><code> \/\/this is relative with the curent size\/zoom<\/code><\/p>\n<p><code> void zoom(float zoomWithThisFactor);<\/code><\/p>\n<p><code> \/\/if set to true the image will be zoomed to fit the widget<\/code><\/p>\n<p><code> void setZoomToFit(bool ztf);<\/code><\/p>\n<p><code>signals:<\/code><\/p>\n<p><code>public slots:<\/code><\/p>\n<p><code>protected:<\/code><\/p>\n<p><code> \/\/custom drawing rutine<\/code><\/p>\n<p><code> void paintEvent(QPaintEvent *);<\/code><\/p>\n<p><code> \/\/custom sizeHint method<\/code><\/p>\n<p><code> QSize sizeHint() const;<\/code><\/p>\n<p><code>private:<\/code><\/p>\n<p><code> QImage *image;<\/code><\/p>\n<p><code> float zoomFactor;<\/code><\/p>\n<p><code> float aspectRatio;<\/code><\/p>\n<p><code> bool zoomToFit;<\/code><\/p>\n<p><code> void computeZoomFactorToFitImage();<\/code><\/p>\n<p><code> void adjustSize();<\/code><\/p>\n<p><code>};<\/code><\/p>\n<p><code>#endif \/\/ IMAGEWIDGET_H<\/code><\/p>\n<p>I subclased QWidget and reimpemented 2 methods paintEvent() and sizeHint():<\/p>\n<p><code>void ImageWidget::paintEvent(QPaintEvent *)<\/code><\/p>\n<p><code>{ <\/code><\/p>\n<p><code> int x,y,w,h;<\/code><\/p>\n<p><code> \/\/compute the image displaty size by multipling with the zoom factor<\/code><\/p>\n<p><code> w=image-&gt;width()*zoomFactor;<\/code><\/p>\n<p><code> h=image-&gt;height()*zoomFactor;<\/code><\/p>\n<p><code> \/\/compute the x and y offset<\/code><\/p>\n<p><code> x=(rect().width()-w)\/2;<\/code><\/p>\n<p><code> y=(rect().height()-h)\/2;<\/code><\/p>\n<p><code> \/\/if from some strange bug we get a negative value, <\/code><\/p>\n<p><code> \/\/probly this must be changed to an assert or thow and error and fix the eventual problem<\/code><\/p>\n<p><code> x=qMax(x,0);<\/code><\/p>\n<p><code> y=qMax(y,0);<\/code><\/p>\n<p><code> QRectF target(x,y,w,h);<\/code><\/p>\n<p><code> QPainter p(this);<\/code><\/p>\n<p><code> p.drawImage(target,*image);<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>QSize ImageWidget::sizeHint() const<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code> if(image!=NULL)<\/code><\/p>\n<p><code> {<\/code><\/p>\n<p><code> QSize hint= image-&gt;size();<\/code><\/p>\n<p><code> hint=zoomFactor*hint;<\/code><\/p>\n<p><code> return hint;<\/code><\/p>\n<p><code> }else<\/code><\/p>\n<p><code> return this-&gt;minimumSize();<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>The zoomToFit method will compute the zoom factor to fit the widget and will zoom the image<\/p>\n<p><code>void ImageWidget::setZoomToFit(bool ztf)<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code> this-&gt;zoomToFit=ztf;<\/code><\/p>\n<p><code> if(zoomToFit)<\/code><\/p>\n<p><code> {<\/code><\/p>\n<p><code> computeZoomFactorToFitImage();<\/code><\/p>\n<p><code> }<\/code><\/p>\n<p><code> else<\/code><\/p>\n<p><code> {<\/code><\/p>\n<p><code> zoomFactor=1.0;<\/code><\/p>\n<p><code> }<\/code><\/p>\n<p><code> adjustSize();<\/code><\/p>\n<p><code> this-&gt;update();<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>void ImageWidget::computeZoomFactorToFitImage()<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code> \/\/compute the aspect ration of the widget<\/code><\/p>\n<p><code> \/\/cast t float to get a float result, if forget (float) you will get<\/code><\/p>\n<p><code> \/\/a int value<\/code><\/p>\n<p><code> float ratio=rect().width()\/(float)rect().height();<\/code><\/p>\n<p><code> if(ratio&gt;aspectRatio)<\/code><\/p>\n<p><code> {\/\/the widget width is larger relative of the image wirth<\/code><\/p>\n<p><code> zoomFactor=rect().height()\/(float)image-&gt;height();<\/code><\/p>\n<p><code> }<\/code><\/p>\n<p><code> else<\/code><\/p>\n<p><code> {<\/code><\/p>\n<p><code> zoomFactor=rect().width()\/(float)image-&gt;width();<\/code><\/p>\n<p><code> }<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>void ImageWidget::adjustSize()<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code> resize(this-&gt;sizeHint());<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>You can find all the code on source forge in a Image viewer application, the application is licnsed under GPL v3 , if someone needs the code under other license email me and i can give the rights<\/p>\n<p>UPDATE:<\/p>\n<p>download<\/p>\n<p>svn co https:\/\/kisviwer.svn.sourceforge.net\/svnroot\/kisviwer kisviwer<\/p>\n<p>you must use a svn client or the svn command line application, if in linux install svn and paste that line in the terminal,and a drectory kisviwer will appear with the code,windws guys google it<\/p>\n<p><img decoding=\"async\" title=\"KIS image viewer\" src=\"http:\/\/ploscariu.com\/wordpress\/wp-content\/uploads\/2010\/04\/snapshot1.png\" alt=\"a simple image viewer ,Qt4\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To display image in Qt4 you can use a QLabel and the setPixmap() method. But if you need something more complex that will keep the aspect ratio and can zoom and rotate you will have to create a custom widget. This is how i did it for a Image viewer application that i make it &hellip; <a href=\"http:\/\/ploscariu.com\/wordpress\/2010\/04\/11\/creating-a-imagepicture-widget-in-qt4-that-keeps-the-aspect-ration-has-zoom-and-rotate-features\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating a Image,Picture widget in Qt4 that keeps the aspect ration has zoom and rotate features<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-programming"],"kl_preview_url":"http:\/\/ploscariu.com\/wordpress\/?p=24&preview=1&kc_p=84be2e8a36","_links":{"self":[{"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=24"}],"version-history":[{"count":0,"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"wp:attachment":[{"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ploscariu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}