Zooming function on a QWidget

后端 未结 2 1759
小蘑菇
小蘑菇 2021-01-12 15:51

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 conn

相关标签:
2条回答
  • 2021-01-12 16:24

    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 
    }
    
    0 讨论(0)
  • 2021-01-12 16:29

    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()"

    0 讨论(0)
提交回复
热议问题