In C++, public
means those members that are accessible from anywhere where the object is visible, private
means that members are accessible only from within other members of the same class or from their friends.
But in Qt, the difference in private slots
and public slots
seem not to exist. I have begun writing Qt in recent days, and I used private slots
all the time.
Someone told me I should use public slots
instead. So now I am puzzled. I can not find reference info in the Qt`s docs.
What's the actual difference between the two types?
there is no harm with using slot as a private member, if you are making use of those slots within the same class. it is just a normal member function there is nothing more than normal function... but if you want use those slot outside of the class then you should declare as a public slots.
In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.
Definition of on the q.t. informal. : in a secret or quiet way All the arrangements were made on the q.t. This information is private, so keep it on the q.t.
This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class. What this means: From another class, you can't call a private slot as a function, but if you emit a signal connected to that private slot, you can invoke it."
From Qt Documentation:
Since slots are normal member functions, they follow the normal C++ rules when called directly. However, as slots, they can be invoked by any component, regardless of its access level, via a signal-slot connection. This means that a signal emitted from an instance of an arbitrary class can cause a private slot to be invoked in an instance of an unrelated class.
What this means: From another class, you can't call a private slot as a function, but if you emit a signal connected to that private slot, you can invoke it.
The private/public access is “checked” by the compiler at compile time, but the signal-slot connection is performed at run-time and slots are invoked by some QMetaObject mechanisms (something like for example invokeMethod).
So the difference is this: private slots are private if called as regular member functions but always "public" for signals to invoke, a good reason is because slots conceptually are public interface, since their main purpose is inter-object communication
Another example about some related “weird” stuff is the call of private virtual functions if they are public in the static type of the pointer that is used to call the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With