Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I make an image resize, when scrolling, with the mouse button in QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

How do I make an image resize, when scrolling, with the mouse button in QGraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 494 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lincoln
    wrote on 29 Jul 2022, 03:56 last edited by
    #1

    Hello guys, I have the following code, what I want to do is enlarge the image or reduce it, depending on how I scroll with the middle mouse button, but doing so deforms the image, and it does not meet what I expected.

    Any suggestions on how I could do it. Thanks in advance.

    My code:

    #pragma once
    
    #include <QMainWindow>
    #include <QEvent>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
      Q_OBJECT
    
    public:
      MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
    
    private:
      Ui::MainWindow *ui;
    
      void loadPictureFromFile() noexcept;
    
      // QObject interface
    public:
      virtual bool eventFilter(QObject *watched, QEvent *event) override;
    };
    
    
    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    
    #include <QFileDialog>
    #include <QWheelEvent>
    #include <QGraphicsSceneWheelEvent>
    
    MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent), ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
      QObject::connect(ui->toolButton, QToolButton::clicked, this, MainWindow::loadPictureFromFile);
      ui->graphicsView->installEventFilter(this);
    
    }
    
    MainWindow::~MainWindow()
    {
      delete ui;
    }
    
    
    void MainWindow::loadPictureFromFile() noexcept
    {
      auto fileName=QFileDialog::getOpenFileName(this, "Open a Image", QDir::currentPath(),
                                   "Images (*.jpg *.png)");
    
      if(fileName.isEmpty())
        return;
    
      QGraphicsScene* scene=new QGraphicsScene(this);
      scene->addPixmap(QPixmap(fileName).scaled(ui->graphicsView->width(),
                                                ui->graphicsView->height(),
                                                Qt::KeepAspectRatio,
                                                Qt::SmoothTransformation));
    
    
      ui->graphicsView->setScene(scene);
    }
    
    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
      if(event->type()==QEvent::Wheel && watched==ui->graphicsView){
          ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
          double scaleFactor = 1.15;
          QGraphicsSceneWheelEvent* wEvent=static_cast<QGraphicsSceneWheelEvent*>(event);
          if(wEvent->delta() > 0)
            ui->graphicsView->scale(scaleFactor,scaleFactor);
          else
            ui->graphicsView->scale(1.0/scaleFactor,1.0/scaleFactor);
          return true;
        }
      return QObject::eventFilter(watched, event);
    }
    
    
    
    

    My form:

    419a812f-9286-4287-97e6-6612830717b5-image.png

    I am using version 6.2 of Qt

    Solitary wolf

    1 Reply Last reply
    0
    • L lincoln
      29 Jul 2022, 16:40

      @Gojir4

      ok thanks, now the problem I have is that when the image is enlarged it looks very pixelated

      G Offline
      G Offline
      Gojir4
      wrote on 2 Aug 2022, 08:15 last edited by Gojir4 8 Feb 2022, 12:09
      #4

      @lincoln Try using an image with high resolution (4K or more) and set the initial scale to 0.5 or anything less than 1.0.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gojir4
        wrote on 29 Jul 2022, 07:06 last edited by
        #2

        hi @lincoln ,
        Are you sure you're casting to the right event type? You should either test for QEvent::GraphicsSceneWheel and cast to QGraphicsSceneWheelEvent, or test QEvent::Wheel and use QWheelEvent. I tried your code with Python and it complains that AttributeError: 'PySide6.QtGui.QWheelEvent' object has no attribute 'delta'. In my side QGraphicsSceneWheelEvent is never triggered. So I suggest casting event to QWheelEvent.

        bool MainWindow::eventFilter(QObject *watched, QEvent *event)
        {
          if(event->type()==QEvent::Wheel && watched==ui->graphicsView){
              ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
              double scaleFactor = 1.15;
              QWheelEvent* wEvent=static_cast<QWheelEvent*>(event);
              if(wEvent->angleDelta().y() > 0)
                ui->graphicsView->scale(scaleFactor,scaleFactor);
              else
                ui->graphicsView->scale(1.0/scaleFactor,1.0/scaleFactor);
              return true;
          }
          return QObject::eventFilter(watched, event);
        }
        
        L 1 Reply Last reply 29 Jul 2022, 16:40
        0
        • G Gojir4
          29 Jul 2022, 07:06

          hi @lincoln ,
          Are you sure you're casting to the right event type? You should either test for QEvent::GraphicsSceneWheel and cast to QGraphicsSceneWheelEvent, or test QEvent::Wheel and use QWheelEvent. I tried your code with Python and it complains that AttributeError: 'PySide6.QtGui.QWheelEvent' object has no attribute 'delta'. In my side QGraphicsSceneWheelEvent is never triggered. So I suggest casting event to QWheelEvent.

          bool MainWindow::eventFilter(QObject *watched, QEvent *event)
          {
            if(event->type()==QEvent::Wheel && watched==ui->graphicsView){
                ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
                double scaleFactor = 1.15;
                QWheelEvent* wEvent=static_cast<QWheelEvent*>(event);
                if(wEvent->angleDelta().y() > 0)
                  ui->graphicsView->scale(scaleFactor,scaleFactor);
                else
                  ui->graphicsView->scale(1.0/scaleFactor,1.0/scaleFactor);
                return true;
            }
            return QObject::eventFilter(watched, event);
          }
          
          L Offline
          L Offline
          lincoln
          wrote on 29 Jul 2022, 16:40 last edited by
          #3

          @Gojir4

          ok thanks, now the problem I have is that when the image is enlarged it looks very pixelated

          Solitary wolf

          G 1 Reply Last reply 2 Aug 2022, 08:15
          0
          • L lincoln
            29 Jul 2022, 16:40

            @Gojir4

            ok thanks, now the problem I have is that when the image is enlarged it looks very pixelated

            G Offline
            G Offline
            Gojir4
            wrote on 2 Aug 2022, 08:15 last edited by Gojir4 8 Feb 2022, 12:09
            #4

            @lincoln Try using an image with high resolution (4K or more) and set the initial scale to 0.5 or anything less than 1.0.

            1 Reply Last reply
            0

            1/4

            29 Jul 2022, 03:56

            • Login

            • Login or register to search.
            1 out of 4
            • First post
              1/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved