简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM