Qt-UI

Signal and Slot Example

Signals and Slots | | MOC

Pattern:

  • Method of class A emits signal.
  • A method of another class B is registered as a receiver for the particular signal.
  • The latter method is said to be a slot that is connected to the signal emitter.
  • Then triggering the signal in class A causes the receiver to invoke the corresponding slot in class B.

Setting up a signal/slot connection:

Step 1: Creating a signal emitter

class A : public QObject
{
   Q_OBJECT;

public:

   void method()
   {
      emit signal();
   }

signals:

   void signal();

};

Step 2: Creating a receiver slot

class B : public QObject
{
public slots:

   void slot();

};

Step 3: Connecting the signal emitter with the signal receiver


A *a = new A;
B *b = new B;

connect(a, SIGNAL(signal()), b, SLOT(slot()));


Signals and Slots | | MOC

Options: