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. What does it mean when an entire function is a slot?
Forum Updated to NodeBB v4.3 + New Features

What does it mean when an entire function is a slot?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.3k 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.
  • C Circuits

    I stumbled across a function which is defined as a slot in the header file.. how can an entire function be a slot? Also, it's a private slot, when would you want a slot to be private?

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

    @Circuits
    What do you mean "how can an entire function be a slot?", a slot is a function (or a lambda)? As for private, is the example Qt 4? I believe one of the changes in Qt 5 for the new signal/slots syntax was that slots now have to be public, but I could be wrong :)

    C 1 Reply Last reply
    3
    • C Circuits

      I stumbled across a function which is defined as a slot in the header file.. how can an entire function be a slot? Also, it's a private slot, when would you want a slot to be private?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by JKSH
      #3

      @Circuits said in What does it mean when an entire function is a slot?:

      how can an entire function be a slot?

      Like @JonB said, a slot is a function that is intended to run when a signal is emitted.

      Could you describe what you think a slot should be?

      Also, it's a private slot, when would you want a slot to be private?

      When you want to implement event-driven logic inside your class, but you don't want the slot function to be called by anyone outside the class.

      @JonB said in What does it mean when an entire function is a slot?:

      I believe one of the changes in Qt 5 for the new signal/slots syntax was that slots now have to be public, but I could be wrong :)

      Signals are now be public; slots are unchanged.

      Qt 4: #define signals protected
      Qt 5: #define signals public

      See

      • https://code.woboq.org/qt5/qtbase/src/corelib/kernel/qobjectdefs.h.html#_M/Q_SIGNALS
      • https://www.kdab.com/wp-content/uploads/stories/slides/DD13/dd13_signalsslots.pdf

      [EDIT: Discussion about public/protected/private forked to https://forum.qt.io/topic/107926/qt-signal-and-slot-internal-connection-details --JKSH]

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      JonBJ 1 Reply Last reply
      5
      • JonBJ JonB

        @Circuits
        What do you mean "how can an entire function be a slot?", a slot is a function (or a lambda)? As for private, is the example Qt 4? I believe one of the changes in Qt 5 for the new signal/slots syntax was that slots now have to be public, but I could be wrong :)

        C Offline
        C Offline
        Circuits
        wrote on last edited by
        #4
        This post is deleted!
        JonBJ 1 Reply Last reply
        0
        • C Circuits

          This post is deleted!

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

          @Circuits
          Hmm, so half the time you have been talking about slots you might mean signals!

          If you say you found it in a header file, maybe you mean you're looking at the declaration but not the definition, which would be empty. I don't know now.

          C 1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #6

            Public slots are also visible to QML code without having to add Q_INVOKABLE.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Circuits
              Hmm, so half the time you have been talking about slots you might mean signals!

              If you say you found it in a header file, maybe you mean you're looking at the declaration but not the definition, which would be empty. I don't know now.

              C Offline
              C Offline
              Circuits
              wrote on last edited by Circuits
              #7

              @JonB No I am being a fool it was a slot. I was confusing updateStatus() with statusUpdate() which is another signal. Sorry bare with me I am new to signals/slots and Qt in general. In general, I would like to be able to send this slot a signal from outside of this class, will it need to be a public slot for that to happen? If I can call it directly from the QML then great but if I have to call it from within the c++ code using this Q_INVOKABLE fcarney referred to than that's fine too, so long as I can call something like:

              onUpdateStatus:
              

              from the QML, eventually, one way or another.

              JonBJ 1 Reply Last reply
              0
              • C Circuits

                @JonB No I am being a fool it was a slot. I was confusing updateStatus() with statusUpdate() which is another signal. Sorry bare with me I am new to signals/slots and Qt in general. In general, I would like to be able to send this slot a signal from outside of this class, will it need to be a public slot for that to happen? If I can call it directly from the QML then great but if I have to call it from within the c++ code using this Q_INVOKABLE fcarney referred to than that's fine too, so long as I can call something like:

                onUpdateStatus:
                

                from the QML, eventually, one way or another.

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

                @Circuits
                OK :) I'm afraid I don't know QML. It won't matter what the slot access is, that's an issue wherever signal & slot are connected. Your job is just to emit the signal.

                C 1 Reply Last reply
                0
                • JonBJ JonB

                  @Circuits
                  OK :) I'm afraid I don't know QML. It won't matter what the slot access is, that's an issue wherever signal & slot are connected. Your job is just to emit the signal.

                  C Offline
                  C Offline
                  Circuits
                  wrote on last edited by Circuits
                  #9

                  @JonB I am just a bit confused. It would seem that I have a signal, which is connect to a signal which is connected to a normal method?? The method these signals seem to be connect to is not a slot. For instance:

                  The header:

                  signals:
                  void gnssStatusChanged(QStringList);
                  

                  It's corresponding c++ file:

                  QObject::connect(m_a, &Application::statusChanged,         this, &GnssPresenter::gnssStatusChanged);        Q_ASSERT(rc); }
                  

                  statusChanged leads to another header:

                  signals:
                  void statusChanged       (QStringList const&);
                  

                  also inside that header this statusChanged seems to be hooked up to a variable called status:

                  class ApplicationInterface : public QObject
                  {
                      Q_OBJECT
                      Q_PROPERTY(QStringList const& status      READ status      NOTIFY statusChanged)
                  

                  and when I right-click status and click find references it shows two of them in another c++ file and corresponding header:
                  from the header:

                   QStringList const& status() const override;
                  

                  from the c++ file:

                  QStringList const& MockGnssApplication::status() const { return m_status;               }
                  

                  the header and c++ file above is where the updateStatus() method I was referring to earlier is located. So like I said, it would seem I have a signal hooked to a signal which is hooked to a normal method which is located in the file where the updateStatus() method (slot) is located.

                  What is confusing me:

                  1. Must a signal hook up to a slot or can it hook up to a normal method?
                  2. What is happening in this line:
                  Q_PROPERTY(QStringList const& status      READ status      NOTIFY statusChanged)
                  

                  this seems to be a signal/slot syntax without actually having a slot.

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

                    Hi,

                    It is allowed and has even a name: signal chaining.

                    This allows to propagate a signal upper while not making the internals public.

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

                    C 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Hi,

                      It is allowed and has even a name: signal chaining.

                      This allows to propagate a signal upper while not making the internals public.

                      C Offline
                      C Offline
                      Circuits
                      wrote on last edited by Circuits
                      #11

                      @SGaist Thanks~ Still confused about what emit does. I found lots of signals declared in headers with no corresponding emit call in the c++ file. Perhaps this emit isn't necessary?

                      In any case, I think what I need to do is generate a new signal and connect it too the updateStatus() slot. Can I just do that directly from the QML, in this case?

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

                        Technically speaking, emit is replaced by nothing (take a look at the macro). However, it does make the code more understandable with regard to what should happen at that point. It make also clear that you are calling a signal.

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

                        JKSHJ 1 Reply Last reply
                        2
                        • SGaistS SGaist

                          Technically speaking, emit is replaced by nothing (take a look at the macro). However, it does make the code more understandable with regard to what should happen at that point. It make also clear that you are calling a signal.

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #13

                          @SGaist said in What does it mean when an entire function is a slot?:

                          Technically speaking, emit is replaced by nothing (take a look at the macro). However, it does make the code more understandable with regard to what should happen at that point. It make also clear that you are calling a signal.

                          +1

                          In other words...

                          void MyClass::func()
                          {
                              emit mySignal(); // [1]
                          
                              mySignal(); // [2]
                          }
                          

                          ... [1] and [2] are exactly the same from a compiler's point of view. However, [2] is clearer to a human reader.

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          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