Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    5
    13
    621
    Loading More Posts
    • 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 last edited by

      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?

      JonB JKSH 2 Replies Last reply Reply Quote 0
      • JonB
        JonB @Circuits last edited by 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 1 Reply Last reply Reply Quote 3
        • JKSH
          JKSH Moderators @Circuits last edited by JKSH

          @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

          JonB 1 Reply Last reply Reply Quote 5
          • C
            Circuits @JonB last edited by

            This post is deleted!
            JonB 1 Reply Last reply Reply Quote 0
            • JonB
              JonB @Circuits last edited by

              @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 Reply Quote 0
              • fcarney
                fcarney last edited by

                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 Reply Quote 0
                • C
                  Circuits @JonB last edited by 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.

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @Circuits last edited by 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 1 Reply Last reply Reply Quote 0
                    • C
                      Circuits @JonB last edited by Circuits

                      @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 Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        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 Reply Quote 2
                        • C
                          Circuits @SGaist last edited by Circuits

                          @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 Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            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

                            JKSH 1 Reply Last reply Reply Quote 2
                            • JKSH
                              JKSH Moderators @SGaist last edited by

                              @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 Reply Quote 2
                              • First post
                                Last post