Results 1 to 8 of 8

Thread: relaying signals

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default relaying signals

    Hello!

    Here is the situation. Object A has a B and B has a C (A -> B -> C). C emits signals that are actually meant for A, but A doesn't know about C, so I can't connect() the signals from C to A in A. B knows about C, but it doesn't know about A, so I can't connect() C to A there either. For now the only solution I see is connecting C to B and reemitting the signal from B to A. Isn't there something better I can do?

    Thanks
    Cruz

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: relaying signals

    You do not need to reemit signal yourself. You can connect signal of C to signal of B. And if you connected signal of B to A's slot, you will automatically receive emit of C's signal
    Qt Code:
    1. B::B()
    2. {
    3. connect( &c, SIGNAL( c_signal_signature ), SIGNAL( b_signal_signature ) );
    4. }
    5.  
    6. A::A()
    7. {
    8. connect( &b, SIGNAL( b_signal_signature ), SLOT( a_slot_signature ) );
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to borisbn for this useful post:

    Cruz (5th April 2010)

  4. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: relaying signals

    Here is the situation. Object A has a B and B has a C (A -> B -> C).
    As far as I understand its something like -
    Qt Code:
    1. class A
    2. {
    3. B objB;
    4. };
    5.  
    6. class B
    7. {
    8. C objC;
    9. }
    10.  
    11. class C
    12. {
    13. }
    To copy to clipboard, switch view to plain text mode 

    If thats the case, you need to provide a function in B to return objC. say C* B::getObjC()
    Then in class A or whereever you can connect
    Qt Code:
    1. connect(objB->getObjC(), SIGNAL(), this, SLOT());
    2. connect(objA->getObjB()->getObjC(), SIGNAL(), objA, SLOT());
    To copy to clipboard, switch view to plain text mode 

    If above is not ur required hierarchy, please mention with class structure.

  5. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: relaying signals

    aamer4yu, Cruz said:
    but A doesn't know about C
    I understood it that A knows NOTHING about C.
    that's why connecting C's signal to B's signal, and connecting B's signal to A's slot - it seems to me the right idea. Isn't it ?

  6. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: relaying signals

    "You can connect signal of C to signal of B." - yeah thanks, I wasn't aware of this. This solution is satisfactory.

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: relaying signals

    I understood it that A knows NOTHING about C.
    I even didnt say A knows anything about C... but in some way A knows that it has to perform some operation based on a given signal.
    It depends on where you perform the connect operation. If you connect in main window class,, then I guess it may know about A,B and C.

    I just gave a way where you need not put extra signal in class B. Just imagine if A->B->C->D->E->F->G was the case, and you wanted to emit signal from G and process in A... what technique would you have used ?
    Would you put a signal all the way from B to F ??

  8. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: relaying signals

    Quote Originally Posted by aamer4yu View Post
    Would you put a signal all the way from B to F ??
    Good point. However, the solution you suggested requires that A knows about C, or about G in the case of the extreme example.

    Qt Code:
    1. connect(objA->getObjB()->getObjC(), SIGNAL(), objA, SLOT());
    To copy to clipboard, switch view to plain text mode 

    requires A to include C's header so that it knows about its signals, right? And that's also not very pretty, because B was supposed to hide C from A. So if neither this, nor the signal relaying are perfectly clean solutions for this kind of problem, what else can be done?

  9. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: relaying signals

    requires A to include C's header so that it knows about its signals, right?
    Not necessarily,,, you can make the connection from your main class,,, which knows about A,B and C.
    Also if you are making connection inside A, then A in someway knows about the signal, doesnt it ?? atleast the name and signature

Similar Threads

  1. conflict between boost signals and Qt signals
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 09:46
  2. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18
  3. I need help for signals
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2007, 16:43
  4. Where are signals?
    By PanzerMaus in forum Newbie
    Replies: 1
    Last Post: 12th July 2006, 23:05
  5. KDE Signals
    By chombium in forum KDE Forum
    Replies: 1
    Last Post: 25th January 2006, 18:45

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
  •  
Qt is a trademark of The Qt Company.