'Error in setting in event filter : Can not cast to its private base class QObject
I am trying to set event filter for rubber band rectangle zoom in. But getting the following error
widget.cpp:29:42: error: cannot cast 'SchematicDesign' to its private base class 'QObject' schematicdesign.h:13:68: note: constrained by implicitly private inheritance here
widget.h
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QGraphicsView
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QGraphicsScene* scene;
QGraphicsView* view;
};
#endif // WIDGET_H
widget.cpp
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
view = new QGraphicsView(this);
view->setScene(scene);
view->setContextMenuPolicy(Qt::CustomContextMenu);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setBackgroundBrush(Qt::gray);
view->viewport()->installEventFilter(new SchematicDesign()); //Error
}
SchematicDesign.h
class SchematicDesign : public QGraphicsRectItem
public:
explicit SchematicDesign();
explicit SchematicDesign(qreal x, qreal y, qreal width, qreal height,QGraphicsItem *parent = nullptr)
: QGraphicsRectItem(x, y, width, height, parent)
{}
public:
bool eventFilter(QObject *watched, QEvent *event);
};
Schematicdesign.cpp
SchematicDesign::SchematicDesign(){}
bool SchematicDesign::eventFilter(QObject *watched, QEvent *event)
{
bool filterEvent = false;
switch(event->type()) {
case QEvent::MouseButtonPress:
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
rubberBandOrigin = mouseEvent->pos();
rubberBandActive = true;
break;
}
case QEvent::MouseButtonRelease:
{
if (rubberBandActive) {
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
QPoint rubberBandEnd = mouseEvent->pos();
QGraphicsView * view = static_cast<QGraphicsView *>(watched->parent());
QRectF zoomRectInScene = QRectF(view->mapToScene(rubberBandOrigin),
view->mapToScene(rubberBandEnd));
view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
rubberBandActive = false;
}
break;
}
}
return filterEvent;
}
How to solve this issue ? Any help will be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
