Results 1 to 7 of 7

Thread: Send signal from the parent to the child?

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    129
    Thanked 3 Times in 3 Posts

    Question Send signal from the parent to the child?

    I know how to do the opposite,
    you call whenever you want
    Qt Code:
    1. emit save_path(fileName);
    To copy to clipboard, switch view to plain text mode 
    in the children's code, so as to send the QString(or whatever) fileName to the parent.
    In the header of the children, you place
    Qt Code:
    1. signals:
    2. void save_path(QString img_path);
    To copy to clipboard, switch view to plain text mode 
    and in the paren'ts code you connect the signal with a local slot before showing the children:
    Qt Code:
    1. connect(Screenshot,SIGNAL(save_path(QString)),this,SLOT(add_image_from_screenshot(QString)));
    To copy to clipboard, switch view to plain text mode 
    When the signal is sent, the parent's connected slot executes.
    So, this is the way that a child can send to its parent something, but how do I do the opposite? The children doesn't create the parent, so i don't know how and where actually to place the connection so as to make it work :S
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Send signal from the parent to the child?

    In the parent's code do something like this:
    Qt Code:
    1. connect(this,SIGNAL(image_from_screenshot_added(bool),Screenshot,SLOT(save_operation_end(bool)));
    To copy to clipboard, switch view to plain text mode 

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

    hakermania (7th June 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    129
    Thanked 3 Times in 3 Posts

    Unhappy Re: Send signal from the parent to the child?

    i will try it and i'll let you know!


    Added after 1 30 minutes:


    That's the code in the parent's file:
    Qt Code:
    1. Properties = new properties(this);
    2. connect(this,SIGNAL(filename(QString)),Properties,SLOT(filename_sent(QString)));
    3. Properties->show();
    4. emit filename(ui->listWidget->currentItem()->text());
    To copy to clipboard, switch view to plain text mode 
    The signal works fine, but what it actually does is to pass a variable from the parent to the child, but this variable is needed by the constructor of the child already. So, in the code of the child I have a global QString variable named 'image', so, the local child's slot (filename_sent(QString)) is:
    Qt Code:
    1. void filename_sent(QString img){
    2. image = img;
    3. }
    To copy to clipboard, switch view to plain text mode 
    As you may understood, I need the signal to be sent before the constructor runs, so as 'image' to be a valid variable. How can I accomplish this?
    It seems that in Properties->show(); it waits for the constructor to finish execution and then it sends the message. On the other hand I cannot place the emit signal before the ->show(); because there's nobody to receive the message, right?
    Last edited by hakermania; 7th June 2011 at 12:49.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Send signal from the parent to the child?

    Why You don't add second parameter with file name for child constructor ??? And then You don't need signal filename().
    And Properties->show() don't wait for thr constructor finish. Construnctor is finished after line 1.

  6. The following user says thank you to Lesiok for this useful post:

    hakermania (8th June 2011)

  7. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Send signal from the parent to the child?

    Hope you have found a way to do it, just to add one simple point, you can connect any two QObjects they need not be in parent child relationship.

  8. The following user says thank you to Santosh Reddy for this useful post:

    hakermania (8th June 2011)

  9. #6
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    129
    Thanked 3 Times in 3 Posts

    Default Re: Send signal from the parent to the child?

    Yes guys, I finally passed another parameter to child's constructor
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  10. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Send signal from the parent to the child?

    So, this is the way that a child can send to its parent something, but how do I do the opposite? The children doesn't create the parent, so i don't know how and where actually to place the connection so as to make it work :S
    You problem is that you don't have reference(or pointer) to parent in child, there many ways to get parent reference in child, which you can figure out your self

    The best way (in most of cases which I have seen) is to give the responsibility of making connection to the object which creates the parent and child. If parent creates it child internally, then its better that parent takes the responsibility making the connection). This method will prove useful once you a bunch of objects connected to bunch of other object in your app.

Similar Threads

  1. Replies: 5
    Last Post: 21st April 2010, 22:36
  2. Parent/child transformations
    By jano_alex_es in forum Newbie
    Replies: 4
    Last Post: 5th November 2009, 11:13
  3. Replies: 4
    Last Post: 3rd October 2009, 09:19
  4. I need to send a message to parent dialog
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2008, 12:16
  5. resizeEvent from parent to child?
    By ChasW in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2007, 19:21

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.