Solved How to tell in advance if I'll have to call base class implementation of a method/slot?
-
Hi.
So this little thing happened to me. I have a class derived from QTreeView. I implemented a virtual base slot (currentChanged()). And, the automatic view scrolling stopped working.
So in derived method, I called a parent implementation of currentChanged(), and all is fine.I already know of such a thing with events. However, this one happened with a slot. So, my question: is there a way to know beforehand if something will break when deriving from parent methods without calling their base class implementation?
-
@rebus_x said in How to tell in advance if I'll have to call base class implementation of a method/slot?:
So, my question: is there a way to know beforehand if something will break when deriving from parent methods without calling their base class implementation?
You should always call the base class implementation when you override a function to not break the base class' behavior - and this is not Qt specific.
-
@Christian-Ehrlicher
Here's a harder one: how to know whether to call the base method before or after your own code in an override, given that you don't have its source code and the documentation does not make clear what the implementation is doing? ;-) -
@JonB you don't have the source code when you use the distribution provided by your linux flavor :P
If you use the online/offline installer, just make a checkbox next to
sources
or get them later on here https://code.qt.io/cgit/
-
@Christian-Ehrlicher wrote:
and this is not Qt specific
Mine is a question about C++/third-party base classes in general, not Qt where sources are available.
And a beginner like the questioner may not want/be able to dig through source code to understand what it does when wanting a general rule about which side of calling the base method to write his new code.
I know there is no general answer to the question. It is an observation/rhetorical question on the vagaries of C++/an OO language.
-
Thanks everyone for the inputs. So the answer would be
- just to see if anything actually breaks (empirical approach)
- read the docs and hope this is mentioned there (failed in my case ;)
- read sources.
-
- always call the base class except you don't want the behavior of the base class