Planned maintenance: From Sunday 8th December 10:00 CET there will be changes to try and solve the caching issues that have been experienced. If anyone has a problem connecting after this period then please PM @AndyS or any of the moderators.

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



  • 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?



  • @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 :)


  • Moderators

    @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

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



  • This post is deleted!


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



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



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



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



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


  • Lifetime Qt Champion

    Hi,

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

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



  • @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?


  • Lifetime Qt Champion

    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.


  • Moderators

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


Log in to reply