[Qt-interest] QPainter, image scaling problem

Hannu Shemeikka hps at shemeikka.org
Wed Oct 13 08:43:23 CEST 2010


Thanks,

Your advice helped me. I'm now using QGraphicsPixmapItem, QGraphicsView
and OpenGL viewport:
this->view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));

Scaling is done with QMatrix:
this->matrix.scale(scaleFactor,scaleFactor); //(QMatrix)
this->view->setMatrix(this->matrix);

Now I can properly zoom in & zoom out without any performance issues.
Though I don't know if this is the best way to do this, it is working
and doesn't seem to cause any problems.

- Hannu

On Sun, 2010-10-10 at 19:24 +0400, Constantin Makshin wrote:
> 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?
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest





More information about the Qt-interest-old mailing list