Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsView fit image



  • I have made an imageviewer with QGraphicsView.
    i wanted the image will fit the view .

    #include "view.h"
    
    #include <QDebug>
    #include <QWheelEvent>
    
    
    view::view(QWidget *parent):QGraphicsView (parent)
    {
        setCacheMode (CacheBackground);
        setViewportUpdateMode (BoundingRectViewportUpdate);
        setRenderHint (QPainter::HighQualityAntialiasing);
        setTransformationAnchor (AnchorUnderMouse);
    }
    
    void view::zoomIn()
    {
        scale(scaleFactor, scaleFactor);
        isFit = false;
    }
    
    void view::zoomOut()
    {
        scale(invScaleFactor, invScaleFactor);
        isFit = false;
    }
    
    void view::zoomNormal()
    {
        resetMatrix();
        isFit = false;
    }
    
    void view::zoomFit()
    {
        fitInView (sceneRect(), Qt::KeepAspectRatio);
        isFit = true;
    }
    
    bool view::eventFilter(QObject *, QEvent *event)
    {
        if (event->type() == QEvent::Wheel)
        {
            QWheelEvent *wheel_event = static_cast<QWheelEvent *> (event);
    
            if (Qt::RightButton & wheel_event->buttons()) { // right button + wheel
                if (wheel_event->delta() > 0) scale(scaleFactor, scaleFactor);
                else if (wheel_event->delta() < 0) scale(invScaleFactor, invScaleFactor);
            }
            else if (wheel_event->modifiers() == Qt::ControlModifier) { // ctrl + wheel
                if (wheel_event->delta() > 0) scale(scaleFactor, scaleFactor);
                else scale(invScaleFactor, invScaleFactor);
            }
            else if (wheel_event->buttons() == 0){ // only wheel
                if (wheel_event->delta() < 0) emit nextImage();
                else if (wheel_event->delta() > 0) emit prevImage();
            }
        }
    
        return false;
    }
    
    void view::mouseDoubleClickEvent (QMouseEvent *event)
    {
        if (event->button () == Qt::LeftButton)
        {
            if (isFit) zoomNormal ();
            else zoomFit ();
        }
    }
    
    
    #pragma once
    
    #include <QGraphicsView>
    #include <QEvent>
    
    class view : public QGraphicsView
    {
        Q_OBJECT
    
    public:
        view(QWidget *parent = nullptr);
    
    public slots:
        void zoomIn();
        void zoomOut();
        void zoomNormal();
        void zoomFit();
    
    private:
        const double scaleFactor = 1.25;
        const double invScaleFactor = 1 / 1.25;
        bool eventFilter(QObject *, QEvent *event);
        void mouseDoubleClickEvent(QMouseEvent *event);
        bool isFit = true;
    
    signals:
        void nextImage();
        void prevImage();
    };
    
    
    
    
    
    

    so i call the zoomfit functio to fit the image. but issue is after fit the image is distorted.
    but if i call the resetMatrix(); then all good but image is zoomed.

    I want a clear image with fit.

    here is pic wiin detored , fit and not zoomed
    0_1553424195695_Pictur.png

    here is one not fit ,zooomed, not distorted
    0_1553424251522_Pictur9.png


Log in to reply