[Qt-interest] QPainter, image scaling problem

Constantin Makshin cmakshin at gmail.com
Sun Oct 10 17:24:24 CEST 2010


Origin (the point with zero coordinates) isn't affected by scaling,
i.e. objects (QImage in your case) placed at (0, 0) get only right and
bottom edges moved when the scene is scaled.

So you have some choice:
1) manually calculate coordinates of the destination rectangle's top-left point;
2) since you're using OpenGL, you can put a quad with your QImage as
the texture at (-width / 2, height / 2, 0) coordinates and let OpenGL
perform zooming by changing the projection matrix;
3) use QGraphicsView and put the image into QGraphicsPixmapItem, then
use QGraphicsView functions to zoom.

On Sun, Oct 10, 2010 at 2:05 PM, Hannu Shemeikka <hps at shemeikka.org> wrote:
> Hi,
>
> I need to implement a zoom in & zoom out function in my application.
> Currently the image is displayed in custom QGLWidget which is inserted
> into vertical layout.
>
> Normal situation is displayed in attached image_normal.png. I use
> QPainter to draw QImage. The size is 700x700.
>
> paintEvent-code:
> *****************
> QPainter painter2(this);
> QRectF target(0.0, 0.0, ROWS, COLUMNS); // 700,700
> painter2.setWindow(0,0,ROWS,COLUMNS);   // 700,700
>
> painter2.scale(this->dScaleFactor ,this->dScaleFactor);
> painter2.drawImage(target,*this->grayImage);
> *****************
>
> dScaleFactor is increased by 0.1 every time left mouse button is clicked
> and decreased by 0.1 every right button click.
>
>
> The Problem:
>
> Every time scaling is used, it moves the image a bit further to right,
> outside the view. Attached image_scaled.png show the image position
> after 6 clicks.
>
> The Question:
>
> Is there a way to move the image to left so that the gray stuff
> displayed in both images would stay in center of the view when clicking
> the left mouse button (using the scale-function) ?
>
> Or is there a completely another way to do zoom in & zoom out on
> QPainter?




More information about the Qt-interest-old mailing list