Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt "private slots:" what is this?

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

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

And 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);   ... 
like image 381
Justin Avatar asked Feb 05 '12 07:02

Justin


People also ask

What is private slots in Qt?

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

What are Qt Public slots?

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.

How Qt slots work?

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.


2 Answers

Slots are a Qt-specific extension of C++. It only compiles after sending the code through Qt's 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 linking. The extra keywords are #defined away with the standard preprocessor.

like image 198
Russell Davis Avatar answered Oct 05 '22 23:10

Russell Davis


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

like image 32
Andrew Avatar answered Oct 06 '22 01:10

Andrew