abdolahS
abdolahS

Reputation: 683

Signal/Slot between classes and Design of Classes

suppose I've the following classes :

class A:public QObject{
    Q_OBJECT
...
signals:
   void sendData(QString data);
}

class B:public QObject{
    Q_OBJECT

  public:
     A a;
...
public slots:
  void onSendData(QString);
signals:
   void  sendData(QString data);
}


class C:public QObject{
    Q_OBJECT

  public:
     B b;
...
public slots:
  void onSendData(QString);
signals:
   void sendData(QString data);
}

.
.
.


class MainWindow:public QMainWindow{
    Q_OBJECT

  public:
     LastClass lc;
public slots:
  void onSendData(QString);//show data on ui
}

class A digs data and when it finds a special data it must send it to ui(mainwindow) , so it calls sendData signal, then class B which contains an instance of A, grabs the signal and from it's slot sends it to above class ,...

as you can see, it causes a recursive signal/slot send and receiving which I doubt that it is a good design.

Is it a correct way or must I change the design ? ( it's hard to change design in some circumstances though). (I cannot use inheritance because each class has a different functionality and different functions.)

Upvotes: 0

Views: 247

Answers (1)

Davy
Davy

Reputation: 470

This is not an obligation to connect the signals to a slot, in your case you can directly connect the signals between them. In the class B for example:

CONNECT(a, SIGNAL(sendData(QString)), this, SIGNAL(sendData(QString)));

Alternatively, you can use events so that you don't have to make this chain (but you won't know for sure who is catching the event then). Documentation here.

Upvotes: 1

Related Questions