How to stop propagation of mouse and all events to parent but allow events to the components and its children

50 Views Asked by At
class PageGroupHandler: public QQuickWidget
{
    Q_OBJECT
public:
    PageGroupHandler();
protected:
    bool eventFilter(QObject *obj, QEvent *event);
};

PageGroupHandler::PageGroupHandler()
    : QQuickWidget(QUrl("qrc:/custom/qml/PagesGroups/PageManager.qml"))
{
    this->installEventFilter(this);
}
bool PageGroupHandler::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == this || obj->parent() == this) {
        return QObject::eventFilter(obj, event);
    }
    return true;
}

NOTE: There is no error. I have loaded a qml component, it has some rectangle components, some have mouse event some does not, so when i scroll on it the events works but when there is no scroll area the parent which itself is an scrollarea starts scrolling, its pretty weird.

2

There are 2 best solutions below

1
zonmen On

As I understand when you move mouse, you get that event on most child widget first. So you can just override specific event (e.g. CustomWidget::mouseMoveEvent...) in child widget and NOT call parent implementation (e.g. QWidget::mouseMoveEvent) and parent widget does not receive that event.

0
Mohammad Imran On

Thank you everyone for your kind suggestions, answers and helpful informations. I guess i am not very good at asking questions, but since i have solved the problem, i would to like to answer my question.

Ques: QQuickwidget loads QML component with scrollarea. How to stop unwanted mouse event propagation to qquickwidget parent(QWidget) but allow loaded QML to respond to mouse events.

class PageGroupHandler: public QQuickWidget     
{
protected:
    bool eventFilter(QObject *obj, QEvent *event)
    { 
        return obj == root ? true: QQuickWidget::eventFilter(obj, event);/*step 1*/
    }
};
PageGroupHandler::PageGroupHandler()
    : QQuickWidget(QUrl("qrc:/custom/qml/PagesGroups/PageManager.qml"))
{
    root = rootObject(); /*loaded qml*/
    rootContext()->setContextProperty("pageGroupWindow", this);
    pageGroupSizeType = PageGroupSizeType::Full;
    setSizeZero();

    root->installEventFilter(this); /*step 2*/
};