29

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?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
罗泽轩
  • 1,603
  • 2
  • 14
  • 19
  • possible duplicate of [Qt "private slots:" what is this?](http://stackoverflow.com/questions/9147636/qt-private-slots-what-is-this) – kolen Mar 06 '14 at 15:54

3 Answers3

37

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.

user2448027
  • 1,628
  • 10
  • 11
16

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.

Zlatomir
  • 6,964
  • 3
  • 26
  • 32
  • 7
    Small addendum: for truly private slots one can also use the (undocumented, internal) `Q_PRIVATE_SLOT` mechanism, which allows to define the slot in another -- usually private -- class. – peppe Jun 09 '13 at 16:56
5

@user2448027 answer is correct, but there is a missing point in Qt's design pattern: different applications of private slots vs public slots.

By making slot private you force users of the object to use connect function to call the slot, rather than member access operators(. or ->).

Imagine you have a slow or blocking code in one of the slots of your class. You expect users of the class to move the created object to another thread, so the owner of this object (GUI or some object's related to GUI) would not freeze or block by calling this slot. Here is the point: if the slot is called directly by . or -> operators, it will block. Because the current thread of caller method uses the slot, it can only happen if you use public slot. The solution is to make the slot private, so the user could only call it with connect, but not member access operators(. or ->).

Conclusion:

  • If you have blocking slots, make them private.
  • If the slots are used as simple settings of object property, make them public.
  • If you need some methods with return values or inconstant reference arguments, do not even make them slots (it is nonsense), only public is enough.
MaxPlankton
  • 1,158
  • 14
  • 28
AMCoded
  • 1,374
  • 2
  • 24
  • 39