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. Zooming/Scrolling issue with QWheel Event
QtWS25 Last Chance

Zooming/Scrolling issue with QWheel Event

Scheduled Pinned Locked Moved Solved General and Desktop
qwheelqtexteditzooming
4 Posts 3 Posters 1.6k Views
  • 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.
  • S Offline
    S Offline
    Saviz
    wrote on 22 Dec 2022, 00:00 last edited by
    #1

    Hello everyone,

    I am currently trying to implement a zooming effect for my central QTextEdit by using QWheel event. However, I am facing an issue.

    When I try to zoom in/out, the zoom effect works as expected. But when I use the mouse wheel, the scroll bar is being invoked as well, causing the QTextEdit to scroll up and down. I don't want this to happen as I want the position to remain the same and only to zoom in/out.

    Code of MainWindow.cpp:

    void MainWindow::wheelEvent(QWheelEvent *event)
    {
        bool controlKeyIsHeld = QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
    
        // If vertical mouse wheel goes up, then zoom in.
        if(event->angleDelta().y() > 0 && controlKeyIsHeld)
        {
            ui->QTextEdit_TextEdit->zoomIn(1);
        }
    
        else if(event->angleDelta().y() < 0 && controlKeyIsHeld)
        {
            ui->QTextEdit_TextEdit->zoomOut(1);
        }
    }
    

    Here is the virtual method in MainWindow.h:

    protected:
        void wheelEvent(QWheelEvent *event) override;
    
    J 1 Reply Last reply 22 Dec 2022, 06:14
    0
    • S Saviz
      22 Dec 2022, 00:00

      Hello everyone,

      I am currently trying to implement a zooming effect for my central QTextEdit by using QWheel event. However, I am facing an issue.

      When I try to zoom in/out, the zoom effect works as expected. But when I use the mouse wheel, the scroll bar is being invoked as well, causing the QTextEdit to scroll up and down. I don't want this to happen as I want the position to remain the same and only to zoom in/out.

      Code of MainWindow.cpp:

      void MainWindow::wheelEvent(QWheelEvent *event)
      {
          bool controlKeyIsHeld = QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
      
          // If vertical mouse wheel goes up, then zoom in.
          if(event->angleDelta().y() > 0 && controlKeyIsHeld)
          {
              ui->QTextEdit_TextEdit->zoomIn(1);
          }
      
          else if(event->angleDelta().y() < 0 && controlKeyIsHeld)
          {
              ui->QTextEdit_TextEdit->zoomOut(1);
          }
      }
      

      Here is the virtual method in MainWindow.h:

      protected:
          void wheelEvent(QWheelEvent *event) override;
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 Dec 2022, 06:14 last edited by
      #2

      @Saviz You should subclass QTextEdit and override wheelEvent there instead of doing it in MainWindow.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 23 Dec 2022, 02:05
      3
      • J jsulm
        22 Dec 2022, 06:14

        @Saviz You should subclass QTextEdit and override wheelEvent there instead of doing it in MainWindow.

        S Offline
        S Offline
        Saviz
        wrote on 23 Dec 2022, 02:05 last edited by
        #3

        @jsulm Thank you so much. Your solution works perfectly.

        Here is the code if anyone is interested:

        QTextEditSubClass.h:

        #ifndef QTEXTEDITSUBCLASS_H
        #define QTEXTEDITSUBCLASS_H
        
        #include <QTextEdit>
        
        class QTextEditSubClass : public QTextEdit
        {
            Q_OBJECT
        
        public:
            explicit QTextEditSubClass(QWidget *parent = 0);
            ~QTextEditSubClass();
        
            // QWidget interface
        protected:
            void wheelEvent(QWheelEvent *event) override;
        };
        
        #endif // QTEXTEDITSUBCLASS_H
        
        

        QTextEditSubClass.cpp:

        #include "qtexteditsubclass.h"
        #include "qapplication.h"
        #include "qevent.h"
        
        QTextEditSubClass::QTextEditSubClass(QWidget *parent)
        {
        
        }
        
        QTextEditSubClass::~QTextEditSubClass()
        {
        
        }
        
        void QTextEditSubClass::wheelEvent(QWheelEvent *event)
        {
            bool controlKeyIsHeld = QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
        
            // If vertical mouse wheel goes up, then zoom in.
            if(event->angleDelta().y() > 0 && controlKeyIsHeld)
            {
                this->zoomIn(1);
                return;
            }
        
            else if(event->angleDelta().y() < 0 && controlKeyIsHeld)
            {
                this->zoomOut(1);
                return;
            }
        
            QTextEdit::wheelEvent(event);
        }
        

        After creating this sub class, we simply create a new instance of this class and pass it to the mainwindow central widget.

        Hope this helps.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Musshca
          wrote on 17 Feb 2023, 08:16 last edited by
          #4

          Thank buddy for your searching ! But could you please share how do you implement in mainwindow?

          I am using a qtextedit in my mainwindow where I want to zoom and when I implement by creating an instance of this class i am getting errors.

          Thank you.

          1 Reply Last reply
          0

          • Login

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