Results 1 to 3 of 3

Thread: passing SIGNAL from a object contained in another... is there a nicer way?

  1. #1
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default passing SIGNAL from a object contained in another... is there a nicer way?

    Hi!

    I'm just wonder how people normally do when they have a instance A lying in a instance B which lies in Frame C. Instance D lies in C and needs signals from A. But since A is not known by C or D, the signal from A must be passed by B... do I need to do like this:
    ( excuse the bad indentation on the code, I've tried to prettify it )

    who knows who:
    A<------B<------- [C]--------->D

    propagation of signal a1:
    [A: send SIGNAL a1 ]->[B: recieve in SLOT, pass SIGNAL b1 (saying the same thing)]-------------->D[recieves SIGNAL b1 which actually is a1]

    Qt Code:
    1. /** class A ******************/
    2. class A{
    3. signals:
    4. void a1();
    5. }; class A
    6.  
    7. /** class B *******************/
    8. class B{
    9.  
    10. /** connect in constructor */
    11. B( )
    12. {
    13. connect ( &a, SIGNAL(a1()), this, SLOT(pass_signalA1()));
    14. }// B ( )
    15.  
    16. slots:
    17.  
    18. /** a slot only passing the signal */
    19. void pass_signalA1()
    20. {
    21. emit b1();
    22. }// pass_signalA1()
    23.  
    24. signals:
    25.  
    26. /** the signal, actually the a1 signal passed on */
    27. void b1();
    28. A a;
    29. }; // class B
    30.  
    31. /*** class C **************/
    32. class C{
    33.  
    34. /** connect B and D */
    35. C ( )
    36. {
    37. connect ( &b, signal(b1()), &d, SLOT(Dslot()));
    38. }// C( )
    39.  
    40. B b;
    41. D d;
    42. }; //class C
    43.  
    44. /*** class D *****/
    45. class D{
    46.  
    47. /** slot recieving the signal b1 */
    48. slots:
    49. void Dslot();
    50.  
    51. };//class D
    To copy to clipboard, switch view to plain text mode 
    Maybe I should show some code instead, but I give this a chance.

  2. The following user says thank you to pir for this useful post:

    mhoover (17th May 2006)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing SIGNAL from a object contained in another... is there a nicer way?

    You don't have to implement a slot to pass the signal, you can do this instead:
    Qt Code:
    1. connect( &a, SIGNAL( a1() ), this, SIGNAL( b1() ) );
    To copy to clipboard, switch view to plain text mode 
    Now whenever "this" receives the a1() signal, it will emit b1().

  4. The following 2 users say thank you to jacek for this useful post:

    mhoover (17th May 2006), pir (20th May 2006)

  5. #3
    Join Date
    May 2006
    Location
    Stockholm, Sweden
    Posts
    81
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: passing SIGNAL from a object contained in another... is there a nicer way?

    nice, thanks! So it was worth the trouble writing this message after all.

Similar Threads

  1. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  2. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  3. Replies: 3
    Last Post: 15th April 2007, 19:16

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.