can a slot be private ....if it can be then does it has the same meaning as private variables or function ??..
Printable View
can a slot be private ....if it can be then does it has the same meaning as private variables or function ??..
yes, slots can be private.
read more about signal and slots http://doc.trolltech.com/4.4/signalsandslots.htmlQuote:
Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them.
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.
i was confused with the same thing..in my case the slot is private and i am calling it from a different class i.e the base class of the class in which the slot is ...i was surprised to see that slot being private is also visible to the base class..anyway thnks...but still :eek:
can you show the code?
Code:
#include "Control.h" #include <QGraphicsScene> { Q_OBJECT public: Control *contrl; MainScene(); virtual ~MainScene(); }; MainScene::MainScene() { .. connect(contrl,SIGNAL(acceptButtonConnected()),contrl,SLOT(clickAcceptButton())); .. }
Code:
{ Q_OBJECT public: Control(); virtual ~Control(); private slots: void clickAcceptButton(); signals: void acceptButtonClicked(); }; void Control::clickAcceptButton() { emit this->acceptButtonClicked(); }
of course this slot will work, because you connect signal from control with slot which is located in control.
try to callin mainWindow and you get error.Code:
control->clickAcceptButton()
Remember that sections "private", "public" and "protected" are meaningful only during compilation - it is the compiler who decides whether one can access a particular method or field or not. But signal-slot connections are performed during runtime by the meta object system and are always called by the receiver object itself, so it will never violate a private slot. Bottom line - slots are private when called as regular methods but always public when referenced by QMetaObject::invokeMethod().