Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zooming function on a QWidget

Tags:

qt

qt4

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?

like image 848
schmimona Avatar asked Jul 11 '11 12:07

schmimona


1 Answers

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 
}
like image 75
Raiv Avatar answered Sep 30 '22 02:09

Raiv