Example of calling a function to parent?
-
Hi,
The Qt documentation contains quite a lot of examples using signals and slots. You should go through the ones from the widgets module since it's the one you are implementing your application with.
-
Hi,
The Qt documentation contains quite a lot of examples using signals and slots. You should go through the ones from the widgets module since it's the one you are implementing your application with.
@SGaist said in Example of calling a function to parent?:
The Qt documentation contains quite a lot of examples using signals and slots
Didn't find a full working example, only fragments of code I 'm trying to put together.
If you have found a full working example please post it! -
These lines are to be found in the class declaration.
-
And now this:
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
I modified it to (this code is in the constructor of FormB):
connect(&parent, &parent::valueChanged, this, &FormB::setValue);
???
It's wrong...The 'parent' I mean.
The sender is FormB...so.. -
And now this:
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
I modified it to (this code is in the constructor of FormB):
connect(&parent, &parent::valueChanged, this, &FormB::setValue);
???
It's wrong...The 'parent' I mean.
The sender is FormB...so..@Panoss
Everything is upside down :)let's recap, you want the main window (formA) to be inform when a value has changed in second window (formB)
The main window register to receive a signal from the second window.
In formA:
connect(formB, &FormB::valueChanged, this , &FormA::valueChangedInFormB);
-
@mpergand it works!!!
Thank you all!The actual name of FormA is 'ArticlesWindow' and of FormB 'positionsForm'.
(FormB is the sender)
I post the code in case someone needs it:articleswindow.h private slots: void positionChanged(); articleswindow.cpp void ArticlesWindow::positionChanged(){ qDebug() << "ArticlesWindow::positionChanged"; } void ArticlesWindow::openPositionsForm(){ class positionsForm *form = new class positionsForm(model, this); connect(form, &positionsForm::valueChanged, this , &ArticlesWindow::positionChanged);
And:
positionsform.h public slots: void setValue(int value); signals: void valueChanged(int newValue); positionsform.cpp void positionsForm::setValue(int value) { emit valueChanged(value); }
-
@mpergand it works!!!
Thank you all!The actual name of FormA is 'ArticlesWindow' and of FormB 'positionsForm'.
(FormB is the sender)
I post the code in case someone needs it:articleswindow.h private slots: void positionChanged(); articleswindow.cpp void ArticlesWindow::positionChanged(){ qDebug() << "ArticlesWindow::positionChanged"; } void ArticlesWindow::openPositionsForm(){ class positionsForm *form = new class positionsForm(model, this); connect(form, &positionsForm::valueChanged, this , &ArticlesWindow::positionChanged);
And:
positionsform.h public slots: void setValue(int value); signals: void valueChanged(int newValue); positionsform.cpp void positionsForm::setValue(int value) { emit valueChanged(value); }
-
It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??
-
Why is this wrong?
Every time I want to open the form I create an instance of it 's class.
When I close the form the instance is destroyed, right?What 's the correct way?
-
Why is this wrong?
Every time I want to open the form I create an instance of it 's class.
When I close the form the instance is destroyed, right?What 's the correct way?
@Panoss said in Example of calling a function to parent?:
Every time I want to open the form I create an instance of it 's class.
Do you also delete the instance when you do not need it anymore?
-
@Panoss said in Example of calling a function to parent?:
Every time I want to open the form I create an instance of it 's class.
Do you also delete the instance when you do not need it anymore?
@jsulm said in Example of calling a function to parent?:
@Panoss said in Example of calling a function to parent?:
Every time I want to open the form I create an instance of it 's class.
Do you also delete the instance when you do not need it anymore?
That's what @Panoss should find out for himself by looking at the sender() on multiple messages.
-
@Panoss
Add sender() and look if it's different objects
qDebug() << "ArticlesWindow::positionChanged"<<sender();@mpergand said in Example of calling a function to parent?:
@Panoss
Add sender() and look if it's different objects
qDebug() << "ArticlesWindow::positionChanged"<<sender();This is the output:
updateParent called!! ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm") updateParent called!! ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
-
Why is this wrong?
Every time I want to open the form I create an instance of it 's class.
When I close the form the instance is destroyed, right?What 's the correct way?
@Panoss said in Example of calling a function to parent?:
When I close the form the instance is destroyed, right?
No, that depends on what you mean/do by "close". And we don't see that in your shown code?
It works but the message is printed twice, like if function ArticlesWindow::positionChanged is called twice??
Does the "twice" happen from the very first time you create the form or after a second open? Are the number calls equal to the number of times you create the form? If it stays at "twice" always, that alters what i would look for in the code.
-
@Panoss said in Example of calling a function to parent?:
Every time I want to open the form I create an instance of it 's class.
Do you also delete the instance when you do not need it anymore?
@jsulm said in Example of calling a function to parent?:
@Panoss said in Example of calling a function to parent?:
Every time I want to open the form I create an instance of it 's class.
Do you also delete the instance when you do not need it anymore?
When I close the form instance (clicking on the close button or with form->close) doesn't it get deleted?
e.g. with this code:
void addArticle::on_cancelButton_clicked() { this->close(); }