QGraphicsView Inheritance
-
I am trying to implement a mouse scroll zoom function as a child class for QGraphicsView.
When compiling I get the error: " error C2247: 'QObject::setObjectName' not accessible because 'CustomGraphicsView' uses 'private' to inherit from 'QGraphicsView'".This occurs for every function call from QGraphicsView.
I used QtCreator's promote to to change my qgraphicsview to my customgraphicsview class.
My code is as follows:customgraphicsview.h
#ifndef CUSTOMGRAPHICSVIEW_H #define CUSTOMGRAPHICSVIEW_H #include <QGraphicsView> #include <QtWidgets> #include <QWidget> class CustomGraphicsView : QGraphicsView { public: CustomGraphicsView(QWidget* parent = 0); protected: virtual void wheelEvent(QWheelEvent *event); private: const double scaleFactor = 1.2 }; #endif // CUSTOMGRAPHICSVIEW_H
customgraphicsview.cpp
#include "customgraphicsview.h" CustomGraphicsView::CustomGraphicsView(QWidget *parent) : QGraphicsView(parent) { } void CustomGraphicsView::wheelEvent(QWheelEvent *event) { setTransformationAnchor(QGraphicsView::AnchorViewCenter); if (event->delta() > 0) { scale(scaleFactor, scaleFactor); } else { scale(1/scaleFactor, 1/scaleFactor); } }
I am currently on Qt 5.7 using MSVC.
-
change inheritance to public:
class CustomGraphicsView :
public
QGraphicsView