What does it mean when an entire function is a slot?
-
@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 :) -
@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]
-
@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.
-
@JonB No I am being a fool it was a slot. I was confusing
updateStatus()
withstatusUpdate()
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 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 theupdateStatus()
method (slot) is located.What is confusing me:
- Must a signal hook up to a slot or can it hook up to a normal method?
- 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.
-
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 ofsignals
declared in headers with no correspondingemit
call in the c++ file. Perhaps thisemit
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? -
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. -
@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.