Qt "private slots:" what is it?

I understand how to use it, but the syntax of this bothers me. What is a "private slots:" do?

I have never seen something between the private and: keywords in a class definition before. Is there some weird C ++ magic here?

And an example here:

#include <QObject> class Counter : public QObject { Q_OBJECT public: Counter() { m_value = 0; } int value() const { return m_value; } public slots: void setValue(int value); ... 
+50
c ++ qt signals-slots
Feb 05 '12 at 7:19
source share
3 answers

Slots are a Qt-specific extension of C ++. It compiles only after sending the code through the Qt preprocessor, the meta-object compiler (moc). See http://doc.qt.io/qt-5/moc.html for documentation.

Edit: As Frank points out, moc is only required for the connection. Additional keywords #defined away with a standard preprocessor.

+38
Feb 05 2018-12-12T00:
source share

Keywords such as public , private are ignored for Qt slots. All slots are actually public and can be connected

+11
Feb 05 '12 at 7:29
source share

Declaring slots as private means that you cannot refer to them from the context in which they are private, like any other method. Therefore, you cannot pass the address of the private slots to connect .

If you declare a signal and slot as private, you say that only this class can control then.

In addition, other answers are also mentioned:
- you can still connect private signals and slots from the outside using tricks
- signals and slots are empty macros and do not violate the locale

0
Feb 23 '17 at 19:29
source share



All Articles