PDA

View Full Version : Accessing "log-class" from other classes



darksaga
23rd May 2007, 18:25
Hi guys,

if my class hierarchy would look like this:



_ mainWindow
|
-------------------------
| | |
class_A class_B logClass
| |
class_C class_D
|
class_E


and my logClass has the following slot:


public slots:
void setLog(QString &str, int &logType);


what is the best way to connect a signal from within class_E or class_D with my logClass???

I hope you get what I have problems with....

Michiel
23rd May 2007, 18:57
No, I'm not sure what the problem is.

First of all, you don't connect signals/slots of classes, but of objects. If you have a Class_D instance and a logClass instance, you can simply use the connect function:

connect(class_D_instance, SIGNAL(theSignal(QString&, int&)), logClassInstance, SLOT(setLog(QString&, int&)));

marcel
23rd May 2007, 19:08
If you don't have access to them, as Michiel said, you can do the following:

obj E -> obj C -> obj A -> main window -> log class

"->" means that the the left object has a signal connected to a slot in the right object.

Regards

jpn
23rd May 2007, 19:18
There is also a technique called "signal chaining" which means you can connect a signal to another signal. This saves you from implementing slot stubs in the intermediate classes. All you need to do is to declare signals for the intermediate classes and establish connections between the corresponding objects.

darksaga
23rd May 2007, 19:42
First of all, you don't connect signals/slots of classes, but of objects. If you have a Class_D instance and a logClass instance, you can simply use the connect function:



Yeah I know, just forgot to write instance of class_x.


If you don't have access to them, as Michiel said, you can do the following:

obj E -> obj C -> obj A -> main window -> log class

"->" means that the the left object has a signal connected to a slot in the right object.

Regards

thats exactly what i meant:

I would like to avoid looping the signal/slots through all objects/classes.

I thought, maybe there was a nifty trick to do it in a better manner using the QT signal/slot mechanism?



There is also a technique called "signal chaining"


Could you post a little example?

jpn
23rd May 2007, 22:06
Could you post a little example?


connect(e, SIGNAL(somethingHappened(QString, int)), c, SIGNAL(somethingHappened(QString, int)));
...
connect(c, SIGNAL(somethingHappened(QString, int)), a, SIGNAL(somethingHappened(QString, int)));
...
connect(a, SIGNAL(somethingHappened(QString, int)), log, SLOT(setLog(QString, int)));

This way, "e" and "log" doesn't have to know anything about each other nor do you have to have pointers to both of them anywhere to establish a direct connection. C could for example establish connection between "this" and E (where E is created), or alternatively E could establish connection between itself and it's parent (C).

darksaga
23rd May 2007, 22:31
thx alot :)

will give this a try.