PDA

View Full Version : private slots ??



salmanmanekia
6th August 2008, 12:50
can a slot be private ....if it can be then does it has the same meaning as private variables or function ??..

spirit
6th August 2008, 12:56
yes, slots can be private.

Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them.
read more about signal and slots http://doc.trolltech.com/4.4/signalsandslots.html

Nithya
6th August 2008, 12:59
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.

salmanmanekia
6th August 2008, 13:15
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:

spirit
6th August 2008, 13:35
can you show the code?

salmanmanekia
6th August 2008, 13:40
#include "Control.h"
#include <QGraphicsScene>

class MainScene:public QGraphicsScene
{
Q_OBJECT
public:
Control *contrl;
MainScene();
virtual ~MainScene();

};

MainScene::MainScene()
{
..
connect(contrl,SIGNAL(acceptButtonConnected()),con trl,SLOT(clickAcceptButton()));
..
}


class Control:public QWidget
{
Q_OBJECT
public:
Control();
virtual ~Control();

private slots:
void clickAcceptButton();
signals:
void acceptButtonClicked();
};

void Control::clickAcceptButton()
{
emit this->acceptButtonClicked();
}

spirit
6th August 2008, 13:45
of course this slot will work, because you connect signal from control with slot which is located in control.
try to call
control->clickAcceptButton() in mainWindow and you get error.

wysota
6th August 2008, 14:00
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().