Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to tell in advance if I'll have to call base class implementation of a method/slot?
Qt 6.11 is out! See what's new in the release blog

How to tell in advance if I'll have to call base class implementation of a method/slot?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 893 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rebus_x
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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? ;-)

        J.HilkJ 1 Reply Last reply
        0
        • JonBJ JonB

          @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? ;-)

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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/


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          JonBJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @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/

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @J-Hilk

            @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.

            R 1 Reply Last reply
            0
            • JonBJ JonB

              @J-Hilk

              @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.

              R Offline
              R Offline
              rebus_x
              wrote on last edited by
              #6

              Thanks everyone for the inputs. So the answer would be

              1. just to see if anything actually breaks (empirical approach)
              2. read the docs and hope this is mentioned there (failed in my case ;)
              3. read sources.
              Christian EhrlicherC 1 Reply Last reply
              0
              • R rebus_x

                Thanks everyone for the inputs. So the answer would be

                1. just to see if anything actually breaks (empirical approach)
                2. read the docs and hope this is mentioned there (failed in my case ;)
                3. read sources.
                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                @rebus_x

                1. always call the base class except you don't want the behavior of the base class

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved