事件过滤器设置错误:无法转换为它的私有基 class QObject

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

我正在尝试为橡皮筋矩形放大设置事件过滤器。但是出现以下错误

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;
}

如何解决这个问题?任何帮助将不胜感激。

QGraphicsRectItem 没有继承自 QObject,所以 installEventFilter 将不起作用。

您需要覆盖 sceneEvent(或 sceneEventFilter,如果您确实需要过滤器,但看起来您可能只希望 SchematicDesign 自行处理事件)并使用 installSceneEventFilter。来自 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().