Example of calling a function to parent?
-
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(); }
-
@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(); }
@Panoss
Only if you have the "delete on close" attribute set on the window, I can't recall right now its name. [UPDATE: Qt::WA_DeleteOnClose].But I would know (I think) if you answered:
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.
which is why I asked that.....
-
@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.
@JonB said in Example of calling a function to parent?:
@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?
From the very first.
@JonB said in Example of calling a function to parent?:
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.You mean number of calls to the function?
I only call it once. Now, why it acts like if I call it twice, I have no idea!! -
@JonB said in Example of calling a function to parent?:
@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?
From the very first.
@JonB said in Example of calling a function to parent?:
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.You mean number of calls to the function?
I only call it once. Now, why it acts like if I call it twice, I have no idea!!@Panoss
YourArticlesWindow::openPositionsForm()
goesclass positionsForm *form = new class positionsForm(model, this);
. I want to know how many times you callopenPositionsForm()
, I believe you create each time you do something like click on something. I want to know whether the number of times you callopenPositionsForm()
(number of times you click to open it??) is or is not equal to the number of times you reportupdateParent called!! ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
gets output. If you open & close the "positions form" 10 times do you get 10 "updateParent called!!" output?
Meanwhile, where you have
emit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
-
@Panoss
YourArticlesWindow::openPositionsForm()
goesclass positionsForm *form = new class positionsForm(model, this);
. I want to know how many times you callopenPositionsForm()
, I believe you create each time you do something like click on something. I want to know whether the number of times you callopenPositionsForm()
(number of times you click to open it??) is or is not equal to the number of times you reportupdateParent called!! ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
gets output. If you open & close the "positions form" 10 times do you get 10 "updateParent called!!" output?
Meanwhile, where you have
emit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
1st opening of the positionsForm. Edited data and updated: updateParent called!! ArticlesWindow::positionChanged positionsForm(0xc2423e7660, name = "positionsForm") updateParent called!! ArticlesWindow::positionChanged positionsForm(0xc2423e7660, name = "positionsForm") 2nd opening of the positionsForm. Edited data and updated: updateParent called!! ArticlesWindow::positionChanged positionsForm(0xc2423e3980, name = "positionsForm") updateParent called!! ArticlesWindow::positionChanged positionsForm(0xc2423e3980, name = "positionsForm") 3rd opening of the positionsForm. Edited data and updated: updateParent called!! ArticlesWindow::positionChanged positionsForm(0xc2423e86a0, name = "positionsForm") updateParent called!! ArticlesWindow::positionChanged positionsForm(0xc2423e86a0, name = "positionsForm")
So no matter how many times I open (and close) the positionsForm (which, as you correctly supposed, is created each time I click on a button), the function (positionChanged) gets called twice.
I 'll have to look more thoroughly in the code, something stupid I must have done somewhere. -
@Panoss
YourArticlesWindow::openPositionsForm()
goesclass positionsForm *form = new class positionsForm(model, this);
. I want to know how many times you callopenPositionsForm()
, I believe you create each time you do something like click on something. I want to know whether the number of times you callopenPositionsForm()
(number of times you click to open it??) is or is not equal to the number of times you reportupdateParent called!! ArticlesWindow::positionChanged positionsForm(0x5de4a24160, name = "positionsForm")
gets output. If you open & close the "positions form" 10 times do you get 10 "updateParent called!!" output?
Meanwhile, where you have
emit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
@JonB said in Example of calling a function to parent?:
@Panoss
Meanwhile, where you haveemit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1 updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1
The signal is emited twice???
-
@JonB said in Example of calling a function to parent?:
@Panoss
Meanwhile, where you haveemit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1 updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1
The signal is emited twice???
-
@JonB said in Example of calling a function to parent?:
@Panoss
Meanwhile, where you haveemit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1 updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1
The signal is emited twice???
@Panoss said in Example of calling a function to parent?:
@JonB said in Example of calling a function to parent?:
@Panoss
Meanwhile, where you haveemit valueChanged(value);
let's have aqDebug() << "emit valueChanged(value);" << value;
updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1 updateParent called!! ArticlesWindow::positionChanged positionsForm(0xba0e7004e0, name = "positionsForm") emit valueChanged(value); 1
The signal is emited twice???
Put a break point and look at the stack trace upon each call.
As @JonB said, nothing is delete until you set it with:
setAttribute(Qt::WA_DeleteOnClose);