Results 1 to 7 of 7

Thread: Accessing "log-class" from other classes

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question Accessing "log-class" from other classes

    Hi guys,

    if my class hierarchy would look like this:

    Qt Code:
    1. _ mainWindow
    2. |
    3. -------------------------
    4. | | |
    5. class_A class_B logClass
    6. | |
    7. class_C class_D
    8. |
    9. class_E
    To copy to clipboard, switch view to plain text mode 

    and my logClass has the following slot:
    Qt Code:
    1. public slots:
    2. void setLog(QString &str, int &logType);
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Accessing "log-class" from other classes

    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&)));
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Accessing "log-class" from other classes

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Accessing "log-class" from other classes

    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.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: Accessing "log-class" from other classes

    Quote Originally Posted by Michiel View Post

    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.

    Quote Originally Posted by marcel View Post
    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?

    Quote Originally Posted by jpn
    There is also a technique called "signal chaining"
    Could you post a little example?

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Accessing "log-class" from other classes

    Quote Originally Posted by darksaga View Post
    Could you post a little example?
    Qt Code:
    1. connect(e, SIGNAL(somethingHappened(QString, int)), c, SIGNAL(somethingHappened(QString, int)));
    2. ...
    3. connect(c, SIGNAL(somethingHappened(QString, int)), a, SIGNAL(somethingHappened(QString, int)));
    4. ...
    5. connect(a, SIGNAL(somethingHappened(QString, int)), log, SLOT(setLog(QString, int)));
    To copy to clipboard, switch view to plain text mode 
    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).
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    darksaga (24th May 2007)

  8. #7
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: Accessing "log-class" from other classes

    thx alot

    will give this a try.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.