Call method in "parent" window from child window.
-
Hello Qt devs!
I'm working on a Qt application which have multiple windows. With this I mean that from my main/parent window I open a new child window. From here I make some changes to a value, which also should be updated in my main/parent window, so when I close my child window, the value is already updated in the parent window. My question is then: How do I make this method call?
I have tried using signals and slots:
@connect(ui.button, SIGNAL(clicked()), this->parent(),
SLOT(updateValue()));@The updateValue() method is placed in my parent window class.
When I say parent and child, it is not something I have set up in any way. I only create a new instance of my child window from my parent. is this the right way to do it?
Thank you for your time!
-
How about:
You give your child window a signal valueChanged(int theValue);
You give your parent window a slot (either public or private, whatever you want) changeValue(int)
At the place where you create the child window (that is: in the parent window), you create a signal-slot connection between the above two.
In your child window, make sure you emit the valueChanged(int) signal whenever you need.
Do not try to make the connection directly between a widget on a child window or widget and some internal slot. It is very bad design. Instead, make sure that each part of your application has a good API, and use that.
-
I'm not sure if I'm doing this correct?
@Setup_selectMachine *selectMachine = new Setup_selectMachine();
QObject::connect(selectMachine, SIGNAL(valueChanged()), this, SLOT(updateValue()));
selectMachine->show();@The above code is where I declare, initialize and show my child window. I've placed the signal-slot connection there as well. "selectMachine" is the name of my child object. "valueChanged()" is the signal in "selectMachine" which should trigger the change. "this" of course refers to this class. "updateValue()" is the slot in this class which I want to be triggered. I am not sending any values directly via the connection, but instead I'm using a resource class which holds all values.
So - Am I doing it the right way?
Thank you for your time!
-
You do know that you are only supposed to give a declaration of a signal, right? No implementation allowed!
So, only do:
@
//MyChildWindow.h
class MyChildWindow: public QDialog
{
Q_OBJECT
/...
signals:
void valueChanged(int);
@Moc will provide an implementation for you.
-
Thank you!
2 things:
- I did make an empty implementation. Deleted it.
- I forgot to declare it a signal in my header file. ("signals:")
I now seems to be working. The variable is not updated properly, but I guess that's due to an error somewhere else.
Thank you so much for your time!
-
[quote author="maxmotor" date="1316523853"]I'm not sure if I'm doing this correct?
@Setup_selectMachine *selectMachine = new Setup_selectMachine();
QObject::connect(selectMachine, SIGNAL(valueChanged()), this, SLOT(updateValue()));
selectMachine->show();@[/quote]You forgot to specify the signal and slot parameters types (int).
@ @Setup_selectMachine *selectMachine = new Setup_selectMachine();
QObject::connect(selectMachine, SIGNAL(valueChanged(int)), this, SLOT(updateValue(int)));
selectMachine->show(); @ -
No I did not - to quote myself:
[quote author="maxmotor" date="1316523853"]I am not sending any values directly via the connection, but instead I'm using a resource class which holds all values.![/quote]
So I don't have to send any parameters with the signal.