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 decide whether a method should be a slot
Qt 6.11 is out! See what's new in the release blog

How to decide whether a method should be a slot

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.5k Views 3 Watching
  • 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    While developing I find myself writing some method and thinking "this might be useful as a slot for some signal", and so declaring it in slots:. I have a fair number of methods declared as slots which I don't yet use as such, I don't know whether that's more than Qt developers would have put in if they were developing my code.

    Obviously if I need it now as a slot there's no debate. But if you don't actually have a use case yet, how do you decide whether or not it would be a good idea to declare it as a slot? Or do you never make it a slot until you actually need it (requires rebuild/breaks compatibility)?

    1 Reply Last reply
    1
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      I normally only declare as slot if I absolutely need it. Qt5 connection syntax made slots fall in terms of actual need to 1% of what they were before.
      Having said that the overhead created by a slot is not super costly and, if unused, most of it can be optimised out by the compiler so it's not a big deal

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JonBJ 1 Reply Last reply
      4
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        I mostly don't declare methods as slots anymore. Overhead, as small as it may be, whether in binary, parsing or mocing, is just unnecessary for me.
        I use them when I need to query them from the meta object, which falls probably waaay under the 1% of cases @VRonin mentioned.

        Don't know about QML though as I'm not a user of it. Does it require C++ methods to be slots for something?

        VRoninV 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          I mostly don't declare methods as slots anymore. Overhead, as small as it may be, whether in binary, parsing or mocing, is just unnecessary for me.
          I use them when I need to query them from the meta object, which falls probably waaay under the 1% of cases @VRonin mentioned.

          Don't know about QML though as I'm not a user of it. Does it require C++ methods to be slots for something?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Chris-Kawa said in How to decide whether a method should be a slot:

          Does it require C++ methods to be slots for something?

          Yes, QML can only access methods:

          • declared as slots
          • declared as Q_INVOKABLE
          • declared as part of a Q_PROPERTY

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • VRoninV VRonin

            I normally only declare as slot if I absolutely need it. Qt5 connection syntax made slots fall in terms of actual need to 1% of what they were before.
            Having said that the overhead created by a slot is not super costly and, if unused, most of it can be optimised out by the compiler so it's not a big deal

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

            @VRonin
            Thanks; I may rethink then and remove a whole bunch of methods I have marked as slots: because I think of them as potentially-slotty ;-)

            Qt5 connection syntax made slots fall in terms of actual need to 1% of what they were before.

            I know how old syntax worked, but I've only ever used new syntax. Could you clarify how you mean this change affected the need to declare slots? Are you just talking about keeping the number down so that the connect() auto-complete doesn't offer too many?

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Disclaimer: probably over-simplifying.
              Qt4 connection used string-lookup to decide what method to execute, declaring a method as slot was the only way to add it to this look-up list to be found. Qt5 connection allowed use use functors and function pointers directly so there is no need for the other overhead

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              JonBJ 1 Reply Last reply
              1
              • VRoninV VRonin

                Disclaimer: probably over-simplifying.
                Qt4 connection used string-lookup to decide what method to execute, declaring a method as slot was the only way to add it to this look-up list to be found. Qt5 connection allowed use use functors and function pointers directly so there is no need for the other overhead

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

                @VRonin
                Ahh, so you mean old style you had to declare as slot else it wouldn't be found in lookup table, now you can get away with just call to non-slot-declared method, right?

                Looks like I was wrong when I said earlier

                Are you just talking about keeping the number down so that the connect() auto-complete doesn't offer too many?

                Creator connect() only offers signals for the signal, that's what I was thinking of, it offers everything for the slot.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Outside of the overhead issue, one reason to keep declaring your functions as slots is to make the intent of your code clear. While you can indeed use function pointers, its always nice to make your code as easy as possible to be read and reasoned about.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonBJ 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

                    Outside of the overhead issue, one reason to keep declaring your functions as slots is to make the intent of your code clear. While you can indeed use function pointers, its always nice to make your code as easy as possible to be read and reasoned about.

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

                    @SGaist
                    Which is what I had been doing. Trouble is, I have ended up with a lot of methods being slots because I could see them being useful as such, though I don't use them that way yet, and I'm beginning to think I'm marking too many that way!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      In that case, only make slots of what you are actually using as slot. You can change that later on.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

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