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. is event always accepted in QGraphicsView::wheelEvent()?
QtWS25 Last Chance

is event always accepted in QGraphicsView::wheelEvent()?

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsviewwheelqgraphicswidget
3 Posts 2 Posters 433 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.
  • O Offline
    O Offline
    odelaune
    wrote on 31 Jan 2024, 11:22 last edited by odelaune
    #1

    Hello, I have a very similar situation to the one described in this post., i.e. I have QGraphicsWidgets in a QGraphicsScene rendered through a QGraphicsView and I would like to zoom in/out using the mouse wheel, except when I scroll on a QGraphicsWidget.

    But, the solution proposed by kshegunov does not work because the QWheelEvent status is always true.

    So I have written a snippet to try to understand what happen. Here is the code
    CMakeLists.txt

    cmake_minimum_required(VERSION 3.5)
    
    project(test LANGUAGES CXX)
    
    set(CMAKE_AUTOMOC ON)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 COMPONENTS REQUIRED Core Widgets)
    
    set(SOURCE_FILES
      main.cpp
      view.cpp
      )
    
    add_executable(test ${SOURCE_FILES})
    
    target_link_libraries(test PRIVATE
      Qt6::Core
      Qt6::Widgets)
    

    main.cpp

    // Qt headers
    #include <QApplication>
    #include <QGraphicsRectItem>
    
    #include "view.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QGraphicsScene scene;
        View v;
        v.setScene(&scene);
    
        QGraphicsRectItem *bkg_item = new QGraphicsRectItem();
        bkg_item->setBrush(QBrush(QColorConstants::White, Qt::SolidPattern));
        bkg_item->setRect(QRectF(0, 0, 1000, 1000));
        scene.addItem(bkg_item);
        
        v.show();
        return app.exec();
    }
    

    view.cpp

    #include "view.h"
    
    #include <QWheelEvent>
    
    View::View(QWidget* parent):
      QGraphicsView(parent)
    {
    }
    
    
    void View::wheelEvent(QWheelEvent* event)
    {
      qDebug() << Q_FUNC_INFO << "accepted?" << event->isAccepted();
      QGraphicsView::wheelEvent(event);
    }
    

    view.h

    #ifndef VIEW_H
    #define VIEW_H
    
    #include <QGraphicsView>
    
    class View : public QGraphicsView
    {
      Q_OBJECT
      
    public:
      explicit View(QWidget* widget = nullptr);
      
    protected:
      void wheelEvent(QWheelEvent* event) override;
    };
      
    #endif // VIEW_H
    

    When I run this code and when I scroll, I always get:
    virtual void View::wheelEvent(QWheelEvent) accepted? true

    So, if the QWheelEvent is already true when I entered in wheelEvent(), how could it be false later, for example in the wheelEvent() of my QGraphicsWidget?

    Am I missing something?

    J 1 Reply Last reply 1 Feb 2024, 06:46
    0
    • O odelaune
      31 Jan 2024, 11:22

      Hello, I have a very similar situation to the one described in this post., i.e. I have QGraphicsWidgets in a QGraphicsScene rendered through a QGraphicsView and I would like to zoom in/out using the mouse wheel, except when I scroll on a QGraphicsWidget.

      But, the solution proposed by kshegunov does not work because the QWheelEvent status is always true.

      So I have written a snippet to try to understand what happen. Here is the code
      CMakeLists.txt

      cmake_minimum_required(VERSION 3.5)
      
      project(test LANGUAGES CXX)
      
      set(CMAKE_AUTOMOC ON)
      
      set(CMAKE_CXX_STANDARD 17)
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      
      find_package(Qt6 COMPONENTS REQUIRED Core Widgets)
      
      set(SOURCE_FILES
        main.cpp
        view.cpp
        )
      
      add_executable(test ${SOURCE_FILES})
      
      target_link_libraries(test PRIVATE
        Qt6::Core
        Qt6::Widgets)
      

      main.cpp

      // Qt headers
      #include <QApplication>
      #include <QGraphicsRectItem>
      
      #include "view.h"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          QGraphicsScene scene;
          View v;
          v.setScene(&scene);
      
          QGraphicsRectItem *bkg_item = new QGraphicsRectItem();
          bkg_item->setBrush(QBrush(QColorConstants::White, Qt::SolidPattern));
          bkg_item->setRect(QRectF(0, 0, 1000, 1000));
          scene.addItem(bkg_item);
          
          v.show();
          return app.exec();
      }
      

      view.cpp

      #include "view.h"
      
      #include <QWheelEvent>
      
      View::View(QWidget* parent):
        QGraphicsView(parent)
      {
      }
      
      
      void View::wheelEvent(QWheelEvent* event)
      {
        qDebug() << Q_FUNC_INFO << "accepted?" << event->isAccepted();
        QGraphicsView::wheelEvent(event);
      }
      

      view.h

      #ifndef VIEW_H
      #define VIEW_H
      
      #include <QGraphicsView>
      
      class View : public QGraphicsView
      {
        Q_OBJECT
        
      public:
        explicit View(QWidget* widget = nullptr);
        
      protected:
        void wheelEvent(QWheelEvent* event) override;
      };
        
      #endif // VIEW_H
      

      When I run this code and when I scroll, I always get:
      virtual void View::wheelEvent(QWheelEvent) accepted? true

      So, if the QWheelEvent is already true when I entered in wheelEvent(), how could it be false later, for example in the wheelEvent() of my QGraphicsWidget?

      Am I missing something?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 1 Feb 2024, 06:46 last edited by
      #2

      @odelaune said in is event always accepted in QGraphicsView::wheelEvent()?:

      status is always true

      That's because @kshegunov calls event->isAccepted() AFTER calling QGraphicsView::wheelEvent(event);

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

      O 1 Reply Last reply 2 Feb 2024, 11:13
      2
      • J jsulm
        1 Feb 2024, 06:46

        @odelaune said in is event always accepted in QGraphicsView::wheelEvent()?:

        status is always true

        That's because @kshegunov calls event->isAccepted() AFTER calling QGraphicsView::wheelEvent(event);

        O Offline
        O Offline
        odelaune
        wrote on 2 Feb 2024, 11:13 last edited by
        #3

        @jsulm
        Indeed! Thank you for your reply.

        1 Reply Last reply
        0
        • O odelaune has marked this topic as solved on 2 Feb 2024, 11:14

        2/3

        1 Feb 2024, 06:46

        • Login

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