Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Slot naming convention
Forum Updated to NodeBB v4.3 + New Features

Slot naming convention

Scheduled Pinned Locked Moved Unsolved The Lounge
11 Posts 9 Posters 2.7k Views 6 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.
  • Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #1

    Out of curiosity - what's the slot naming convention in your projects? Do you name your slots afer what they're supposed to be connected to or after what they do, e.g.

    connect(sender, &Button::clicked, receiver, &Foo::onButtonClicked)
    

    or

    connect(sender, &Button::clicked, receiver, &Foo::selectSomeStuff)
    

    I have my personal pick, but I've seen strong opinions on both. Qt itself is kinda inconsistent, since it has the second style slots, but also supports the first type with the auto-connect feature. I'd like to see some arguments from more people.

    Ronel_qtmasterR JoeCFDJ kshegunovK 3 Replies Last reply
    1
    • Chris KawaC Chris Kawa

      Out of curiosity - what's the slot naming convention in your projects? Do you name your slots afer what they're supposed to be connected to or after what they do, e.g.

      connect(sender, &Button::clicked, receiver, &Foo::onButtonClicked)
      

      or

      connect(sender, &Button::clicked, receiver, &Foo::selectSomeStuff)
      

      I have my personal pick, but I've seen strong opinions on both. Qt itself is kinda inconsistent, since it has the second style slots, but also supports the first type with the auto-connect feature. I'd like to see some arguments from more people.

      Ronel_qtmasterR Offline
      Ronel_qtmasterR Offline
      Ronel_qtmaster
      wrote on last edited by
      #2

      @Chris-Kawa Hi.Personnaly, i name my slots after what they are supposed to.Because the code will be more understandable to other users reading it. For exemple

      connect(&Button::clicked, receiver, &Foo::onButtonClicked)
      connect(&Button::clicked, receiver, &Foo::openFile)

      Do you see the difference?In the second option the Developer directly understand what the click on the button involves

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

        Hi,

        Good question, it depends whether it's an API such as a property where I follow the usual setXXX pattern.

        Otherwise, I usually use meaningful names to keep the code easier to read and understand.

        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
        0
        • Chris KawaC Chris Kawa

          Out of curiosity - what's the slot naming convention in your projects? Do you name your slots afer what they're supposed to be connected to or after what they do, e.g.

          connect(sender, &Button::clicked, receiver, &Foo::onButtonClicked)
          

          or

          connect(sender, &Button::clicked, receiver, &Foo::selectSomeStuff)
          

          I have my personal pick, but I've seen strong opinions on both. Qt itself is kinda inconsistent, since it has the second style slots, but also supports the first type with the auto-connect feature. I'd like to see some arguments from more people.

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          @Chris-Kawa I prefer onButtonClicked. However, as long as the naming convention is consistent in the project, I am ok with it.

          1 Reply Last reply
          0
          • JonBJ JonB referenced this topic on
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            @Ronel_qtmaster What if slot is simple aggregate of 2 or more things that need to happen on a signal e.g.

            void Foo::???()
            {
               readConfig();
               openFile();
               logStuff();
               notifySomething();
            }
            

            Do you name it Foo::readConfigAndOpenfileAndLogStuffAndNotifySomething or onClick?

            @JoeCFD What if you need to connect the same slot to two things, e.g.

            connect(button, &Button::clicked, receiver, &Foo::onButtonClicked)
            connect(action, &Action::triggered, receiver, &Foo::onButtonClicked)
            

            Do you still name it Foo::onButtonClicked or Foo::onButtonClickedOrActionTriggered. What if same slot can be called by multiple more means?

            JoeCFDJ 1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              @Ronel_qtmaster What if slot is simple aggregate of 2 or more things that need to happen on a signal e.g.

              void Foo::???()
              {
                 readConfig();
                 openFile();
                 logStuff();
                 notifySomething();
              }
              

              Do you name it Foo::readConfigAndOpenfileAndLogStuffAndNotifySomething or onClick?

              @JoeCFD What if you need to connect the same slot to two things, e.g.

              connect(button, &Button::clicked, receiver, &Foo::onButtonClicked)
              connect(action, &Action::triggered, receiver, &Foo::onButtonClicked)
              

              Do you still name it Foo::onButtonClicked or Foo::onButtonClickedOrActionTriggered. What if same slot can be called by multiple more means?

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @Chris-Kawa Good question. I do not have a lot of scenarios like you described in my app.

              I might do the following:

              connect(button, &Button::clicked, receiver, &Foo::onButtonClicked)
              connect(action, &Action::triggered, receiver, &Foo::onActionTriggered)
              
              onActionTriggered()
              {
                   onButtonClicked();
              }
              

              or
              for example, if two different signals triggers the same robot move, I may name the slot

              onMoveActivated
              

              If a slot is linked to multiple signals, a generic slot name may be used.

              Axel SpoerlA 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @Chris-Kawa Good question. I do not have a lot of scenarios like you described in my app.

                I might do the following:

                connect(button, &Button::clicked, receiver, &Foo::onButtonClicked)
                connect(action, &Action::triggered, receiver, &Foo::onActionTriggered)
                
                onActionTriggered()
                {
                     onButtonClicked();
                }
                

                or
                for example, if two different signals triggers the same robot move, I may name the slot

                onMoveActivated
                

                If a slot is linked to multiple signals, a generic slot name may be used.

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #7

                I personally use the onButtonClicked style, when the slot performs something, that is clearly and exclusively linked to the signal and the sender.
                onPopupButtonClicked would be an example. In other cases, e.g. where a button causes something to happen, that can also be triggered otherwise, I try to assign the slot a meaningful name, e.g. refreshPageFromDatabase.

                I personally find the on_someSender_someSignal - autoconnect naming not very attractive and I try not to rely on autoconnect. It makes life easy at the beginning, but cumbersome if legacy code gets refactored or re-used.

                Software Engineer
                The Qt Company, Oslo

                1 Reply Last reply
                0
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  I usually go with onButtonClicked if the slot is really only called by a single signal.

                  for example:

                  QObject::connect(qApp, &QGuiApplication::applicationStateChanged, this,     &Backend::onApplicationStateChanged);
                  
                  

                  if multiple signals connect to it, or it's called manually, then a clear name for slot is the way to go. And in that case I don't even declare it as a slot:in the header. But a normal privat/pubic/protected function.


                  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.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #9

                    I guess we are past regular "slots" by now. With the new connect syntax you can connect to any regular member function and don't have to declare these as slots. Except for tutorials I have never used onButtonClicked as slot names (even with the old connect syntax). A member function should be named after the thing it does. Or phrased differently: The general rule (which might have exceptions) is to name any function (member or freestanding) with a verb (one deviation from the rule could be getters with the naming scheme that Qt uses). (To be more precise: a verb in its imperative, active form.)

                    @Chris-Kawa said in Slot naming convention:

                    Do you name it Foo::readConfigAndOpenfileAndLogStuffAndNotifySomething or onClick?

                    I would use separate member functions that do just a single thing and then have multiple connects. If there needs to be a specific order (which is not guaranteed by connect) I might actually fall back to using onButtonClicked (or a lambda inside the connect) as an exception to the rule, where this might only occur by clicking the button. Triggering the same behavior from regular code (instead of clicking a button) you can still call all 4 functions in order. If this is too cumbersome (and occurs often) you should find a better name for the function. Something that is supposed to be called from regular code should not be called onButtonClicked.

                    1 Reply Last reply
                    1
                    • Chris KawaC Chris Kawa

                      Out of curiosity - what's the slot naming convention in your projects? Do you name your slots afer what they're supposed to be connected to or after what they do, e.g.

                      connect(sender, &Button::clicked, receiver, &Foo::onButtonClicked)
                      

                      or

                      connect(sender, &Button::clicked, receiver, &Foo::selectSomeStuff)
                      

                      I have my personal pick, but I've seen strong opinions on both. Qt itself is kinda inconsistent, since it has the second style slots, but also supports the first type with the auto-connect feature. I'd like to see some arguments from more people.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #10

                      @Chris-Kawa said in Slot naming convention:

                      Do you name your slots afer what they're supposed to be connected to or after what they do, e.g.

                      The latter, (almost) universally. A slot is meaningless if you tie it to a specific signal, or to elaborate a bit - a slot is an "action" on the object state of some kind, it shouldn't care to know if a button triggered it or something else, or if it were scheduled manually over the event loop, or w/e.
                      Generally speaking I use what Qt uses for its API - slots are verbs: connect, stop, etc., while signals are adjectives/adverbials connected(), readyToTransmit() ... you get the point.

                      If I have a need for a very specific slot that's tied to the emitter I simply use a lambda (or bind it into the private implementation through the dptr, if I'd chosen to employ it, which I usualy do).

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      2
                      • mzimmersM Offline
                        mzimmersM Offline
                        mzimmers
                        wrote on last edited by
                        #11

                        FWIW, put me in the camp that prefers meaningful slot names. Slots are functions that presumably do something valuable; what they do should be suggested in the function name, as it is with any other function.

                        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