7

I have a QWidget where I am drawing some lines and I would like to enable/implement a zooming function so as to better see the picture which I am drawing. And I want to connect that to the mouse wheel just like in the normal browsers when you can zoom in and out by pressing the 'ctrl' key and turning the mouse wheel.

Is there a default function for that? I tried searching for some examples but without any luck. So how can I do that?

Raiv
  • 5,731
  • 1
  • 33
  • 51
schmimona
  • 849
  • 3
  • 20
  • 40
  • May be consider using `QScrollArea`. Please take a look at http://stackoverflow.com/a/32269574/575491 how zooming could be implemented within `QScrollArea`. – bkausbk Sep 01 '15 at 08:59

2 Answers2

17

Try to reimplement your paintEvent , and apply scale to QPainter before drawing.

class YourClass:public QWidget
{
...
  protected:
     void paintEvent ( QPaintEvent * event );
     void wheelEvent ( QWheelEvent * event );
  private:
     qreal scale;
};

void YourClass::paintEvent ( QPaintEvent * event )
{
    QPainter p;
    p.scale(scale,scale);
// paint here
}
void YourClass::wheelEvent ( QWheelEvent * event )
{
    scale+=(event->delta()/120); //or use any other step for zooming 
}
Raiv
  • 5,731
  • 1
  • 33
  • 51
0

I used this solution before and I have to say that is obsoleted in the las Qt versions. To create this function is all the same except " event->delta()", now it is written "event->angleDelta().y()"