View Full Version : General question about events overriding

14th September 2009, 16:01
what are the best practices when overriding events? Should I always call the base class implementation or not? Sometimes in the docs I read "the default implementation does nothing", but most of the time there is no indication. Of course the safest way is to always call the base class, but that's error-prone.

Also, I believe some base class implementions are meant to be called before my own code, while others should be called after (such as key press events). How do you know that? Nothing better than guessing?

Thank you

14th September 2009, 16:30
Should I always call the base class implementation or not?
I think you shouldn't... It depends on particular situation.

For example, if you want to get zoom by Ctrl+wheel you should reimplement wheelEvent and if the Ctrl modifier is active you zoom you widget, and in the other case (if Ctrl is inactive) you'll call base class implementation and it'll scroll the area. But in first case there is no need to call base impl - you want your widget just to zoom in/out but not to scroll.

And if you want to get smth done by pressing Enter in the widget derived from QLineEdit you should implement "Enter" case but in the other cases you should! call base impl otherwise you widget wouldn't act like a QLineEdit.

And sometimes you need, for example, just to emit some signals or update some data on the event. Then you reimplement event, make you part and the always call base impl.

Hope I helped a bit =)

14th September 2009, 16:35
I asked the question because I was unsure about the QShowEvent. Should I call the base implementation or not? Before or after my own code?

Then I started wondering about other events until I realized that I was not sure about anyone of them!

I mean: I am supposed to browse the Qt sources in order to choose whether to call a base implementation or not? Not that I am afraid of that, just curious about what's the best practice.