Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Some questions about accept() and ignore() of event



  • for example, we overwrite the fonction void mousePressEvent(QMouseEvent *event) for a class.

    if we write: event->accept()
    it means that this event is handled here, do not pass it to others.
    if we write: event->ignore()
    it means that we do nothing here, try to pass this event to other handlers.

    if I am right, plz tell me:
    who are other handlers? the base class?

    if it is the base class, well,
    if(WeDoNothing)
    {event->ignore();}
    and
    if(WeDoNothing)
    {BaseClass:mousePressEvent(event);}

    are they the same?

    can I write like this:
    event->accept();
    BaseClass::mousePressEvent(event);

    u see, i m confused by accept() ignore() and BaseClass::mousePressEvent(event)

    plz help me.

    thx in advance.


  • Moderators

    Basically accept, ignore and calling base implementation are mutually exclusive and you should not mix them.

    accept() - indicates that you want this event and you will handle it your way

    ignore() - indicates that you don't want this event, the event will be propagated to the parent and handled there

    calling base - leave the handling to the default implementation of the base. It might call accept. It might call ignore. It might do additional stuff depending on what kind of event and class are they.

    As for default mouse events - it depends. The "container" widgets like frame will call ignore. Some others, like lineedits etc. will grab focus.
    You can also set a "Qt::WA_NoMousePropagation":http://qt-project.org/doc/qt-5/qt.html#WidgetAttribute-enum attribute on any widget to prevent it from handling their mouse events to the parent.



  • thx.
    but for the ignore() and calling base, i m still confused.

    as u said, ignore() – indicates that you don’t want this event, the event will be propagated to the parent and handled there.

    so event->ignore() and BaseClass::fonction(event) are the same?

    [quote author="Chris Kawa" date="1389122103"]Basically accept, ignore and calling base implementation are mutually exclusive and you should not mix them.

    accept() - indicates that you want this event and you will handle it your way

    ignore() - indicates that you don't want this event, the event will be propagated to the parent and handled there

    calling base - leave the handling to the default implementation of the base. It might call accept. It might call ignore. It might do additional stuff depending on what kind of event and class are they.

    As for default mouse events - it depends. The "container" widgets like frame will call ignore. Some others, like lineedits etc. will grab focus.
    You can also set a "Qt::WA_NoMousePropagation":http://qt-project.org/doc/qt-5/qt.html#WidgetAttribute-enum attribute on any widget to prevent it from handling their mouse events to the parent.
    [/quote]


  • Moderators

    No, base implementation doesn't necessary call ignore.
    Think about it like this:

    ignore() - I don't care about this event and I consciously decide to pass it to the parent.

    base - I might care about this event but I let base implementation decide if it should be kept by my class(via accept) or delegated to the parent(via ignore). It may also do other stuff that I don't want to know about.

    Base implementation is not the same as ignore, It may call ignore. It may call accept just as well. It may do any of those and then some more. It depends on a class and event.
    For example for dragging some additional state is set. Most basic mouse events are just ignored and passed to parent, but that's not a rule for all events and classes.


  • Moderators

    In terms of use cases:

    • I totally don't care about this event: call ignore()
    • I want only my own handling of this event: call accept() and do your stuff
    • I want to keep default behavior but add something to it: call base and then do your stuff, don't call accept() or ignore()
    • I want only the default behavior: only call base or don't overload at all


  • OK i understand now

    thx a lot

    [quote author="Chris Kawa" date="1389127867"]In terms of use cases:

    • I totally don't care about this event: call ignore()
    • I want only my own handling of this event: call accept() and do your stuff
    • I want to keep default behavior but add something to it: call base and then do your stuff, don't call accept() or ignore()
    • I want only the default behavior: only call base or don't overload at all[/quote]

Log in to reply