is event always accepted in QGraphicsView::wheelEvent()?
-
Hello, I have a very similar situation to the one described in this post., i.e. I have
QGraphicsWidget
s in aQGraphicsScene
rendered through aQGraphicsView
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.txtcmake_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? trueSo, if the
QWheelEvent
is already true when I entered inwheelEvent()
, how could it be false later, for example in the wheelEvent() of myQGraphicsWidget
?Am I missing something?
-
@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);
-