我在另一个自定义窗口小部件A
中有自定义窗口小部件B
。窗口小部件A
通常有一个mousePressEvent,我打开一个上下文菜单。但现在,因为它位于小部件B
中,我想打开小部件B
的上下文菜单并拦截事件,以便只有B知道它。
如果我在向B添加A的实例后在B类中使用此代码:
this->installEventFilter(instanceOf_A);
或者
instanceOf_A->installEventFilter(this);
其中
A::eventFilter(QObject *obj, QEvent *e){
if (e->type() == QEvent::MouseButtonPress){
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if (mouseEvent->button() == Qt::RightButton){
qDebug()<<" Right mouse button press detected. \
Return true, now child should not get the mouse event";
return true;
}
}
没有任何作用。此外,return false
,e->accept()
,e->ignore()
无法工作或显示任何影响。你如何设置eventFilter?哪里?在A
或B
?
编辑:替换了e->MouseButtonPress