PDA

View Full Version : Send signal from the parent to the child?



hakermania
7th June 2011, 09:33
I know how to do the opposite,
you call whenever you want

emit save_path(fileName);
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

signals:
void save_path(QString img_path);
and in the paren'ts code you connect the signal with a local slot before showing the children:

connect(Screenshot,SIGNAL(save_path(QString)),this ,SLOT(add_image_from_screenshot(QString)));
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

Lesiok
7th June 2011, 09:37
In the parent's code do something like this:

connect(this,SIGNAL(image_from_screenshot_added(bo ol),Screenshot,SLOT(save_operation_end(bool)));

hakermania
7th June 2011, 11:49
i will try it and i'll let you know!

Added after 1 30 minutes:

That's the code in the parent's file:

Properties = new properties(this);
connect(this,SIGNAL(filename(QString)),Properties, SLOT(filename_sent(QString)));
Properties->show();
emit filename(ui->listWidget->currentItem()->text());
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:

void filename_sent(QString img){
image = img;
}
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?

Lesiok
7th June 2011, 12:23
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.

Santosh Reddy
7th June 2011, 23:19
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.

hakermania
8th June 2011, 07:38
Yes guys, I finally passed another parameter to child's constructor :)

Santosh Reddy
8th June 2011, 07:57
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.