user3443063
user3443063

Reputation: 1615

Signal/slot between 2 objects of different classes

I have a question concerning Signal-Slots:

I have a file userinterface.cpp that has 2 objects of 2 classes MoveSeries and Chart:

MoveSeries * MOVE_SERIES  ;
MOVE_SERIES = new MoveSeries( this);

and

Chart * CHART  ;
CHART  = new Chart ( this);

In my userinterface.cpp I have ui.Diagramm as an object of type Chart.

Now I want to have Chart communicate with MoveSeries. Can I do that with a direct Signal-Slot in userinterface.cpp? Something like that :

 Userinterface.cpp:
   .
   .
   .
    connect(   ui.Diagram   , SIGNAL( send_BarValue( double val   )),
               MOVE_SERIES  , SLOT( on_BarValueReceived (double val) )) ;

   ...

Or can I only have Signal-Slots between MoveSeries <-> Userinterface and Chart <-> Userinterface?

Thank you!

Upvotes: 0

Views: 804

Answers (1)

Tomaz Canabrava
Tomaz Canabrava

Reputation: 2418

Your solution works, but it's written in the wrong way, you cannot pass values on the connect SIGNAL or SLOT calls, just the type.

 connect(ui.Diagram   , SIGNAL( send_BarValue(double)),
           MOVE_SERIES    ,SLOT( on_BarValueReceived (double) )) ;

But this would also be bad, this is Qt4 style connect, and it would compile and run, but if you mistyped anything there you wouldn't get an error in build tyme.

prefer to use the new Signal / Slot syntax:

 connect(   ui.Diagram   , &DiagramClass::end_BarValue,
           MOVE_SERIES    ,&MOVE_SERIESClass::on_BarValueReceived) ;

this way the connections will be compile-time checked, reducing the number of issues you may encounter.

Upvotes: 3

Related Questions