0

I have a question concerning Signal-Slots:

Lets say I have 2 classes CL_A, CL_B

class CL_A : public QObject
{
  Q_OBJECT
private:
  int a,b,c;                  // in reality I have more than 3 variables 
                              // that need to be set
public:
  void set_a ( int par_a);
  void set_b ( int par_b);    // in reality I have more than 3 setters
  void set_c ( int par_c);    // I also have getters and other methods     
  ...
};

CL_B * cl_b = new CL_B();

From CL_B I want to use the setters of CL_A to set the variables a,b,c of class CL_A. In order to do that I set signal-slot-connections for each setter. Now my question is:

Is there a better way to do that instead of having lots of signal-slot-connections (one for each method)?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • 2
    You can pass a pointer to `A` into `B` and call stuff directly. Anyway the situation when an inner object has to set something in the outer object seems like a bad design at the first glance. – SingerOfTheFall Nov 22 '16 at 08:13
  • Are the `CL_A` member variables you want to set by the signals-slots of same type (as it is in the example)? – Tomas Nov 22 '16 at 08:18
  • 3
    This depends a lot of the context. If you set them all at the same time, then you can just pass a structure instead of passing each value independently. But you should edit the question and put some details of what you are actually trying to do. – thuga Nov 22 '16 at 08:20
  • 1
    ***But you should edit the question and put some details of what you are actually trying to do.*** I second this recommendation. Its hard to help without more details and more of an understanding of the exact problem. – drescherjm Nov 22 '16 at 09:17
  • 1
    One question to ask is, if B knows of A (you imply that in the question text) why do you need to use signals and slots, instead of direct calls? There are a lot of possible good reasons, but to answer the question, we/you need to know the reason. – hyde Nov 22 '16 at 09:44

1 Answers1

-1

I assume that those classes are actually QObject descendants, or you can't use signal-slots at all. "Better" way is depending on what actually want to do. Is that should thread-safe and other object is something either global or used by other thread? SHould it be asynchronous? If answer is yes, you either need signals or other method of synchronization, valid for libraries you're using.

QSignalMapper (How works QSignalMapper?) may allow to create some universal single handler to handle multiple events, but how you would generalize those events if your fields aren't some tuple or array?

You may use other C++ patterns. If code is not time-sensitive, but is rather to create code easy to support, you may consider idea to create class that would map a name of field to its address, so you can do something like

cl_b["Speed"] = value;

I recommend try to look in Stephen C. Dewhurst "C++ Gotchas"

Community
  • 1
  • 1
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42