Home > OS >  Error in setting in event filter : Can not cast to its private base class QObject
Error in setting in event filter : Can not cast to its private base class QObject

Time:02-24

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.

CodePudding user response:

QGraphicsRectItem does not inherit from QObject, so installEventFilter will not work.

You'll want to override sceneEvent (or sceneEventFilter, if you actually want a filter, but it seems like you may just want SchematicDesign to handle the events itself) and use installSceneEventFilter. From the documentation:

You can filter events for any other item by installing event filters. This functionality is separate from Qt's regular event filters (see QObject::installEventFilter()), which only work on subclasses of QObject. After installing your item as an event filter for another item by calling installSceneEventFilter(), the filtered events will be received by the virtual function sceneEventFilter(). You can remove item event filters by calling removeSceneEventFilter().

  • Related