Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 314
罗泽轩 Avatar asked Jun 09 '13 15:06

罗泽轩


People also ask

Can slots be private Qt?

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.

How do slots work Qt?

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.

What is private Qt?

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.

What is a private slot C++?

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


2 Answers

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.

like image 172
user2448027 Avatar answered Oct 12 '22 10:10

user2448027


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.

like image 26
Zlatomir Avatar answered Oct 12 '22 10:10

Zlatomir