Does it make any difference, using public slots instead of private slots in Qt?

罗泽轩 picture 罗泽轩 · Jun 9, 2013 · Viewed 15.1k times · Source

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?

Answer

user2448027 picture user2448027 · Jun 9, 2013

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.