Example of calling a function to parent?
-
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?
wrote on 31 Jan 2022, 10:26 last edited by mpergand@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();wrote on 31 Jan 2022, 12:40 last edited by@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?
wrote on 31 Jan 2022, 12:52 last edited by JonB@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?
wrote on 31 Jan 2022, 12:54 last edited by@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(); }
wrote on 31 Jan 2022, 12:56 last edited by JonB@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.
wrote on 31 Jan 2022, 12:57 last edited by Panoss@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!!wrote on 31 Jan 2022, 13:01 last edited by JonB@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;
wrote on 31 Jan 2022, 13:11 last edited by Panoss1st 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;
wrote on 31 Jan 2022, 13:15 last edited by@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???
wrote on 31 Jan 2022, 13:22 last edited by@Panoss
OK to the it-only-ever-gets-called-twice. (You may still have a leak on non-destruction, we'll leave that for now.)So... just how many times does your code call
positionsForm::setValue(int value)
?? Could that be twice, somewhere? -
@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???
wrote on 31 Jan 2022, 13:29 last edited by@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); -
wrote on 31 Jan 2022, 16:32 last edited by Panoss
Whereever in my code I open a form, this:
form.setAttribute(Qt::WA_DeleteOnClose);
is always included.
I dubugged it but didn't find anything.
Anyway, as it's not causing any issues I 'll leave as is.
Better update twice than none! ): -
Whereever in my code I open a form, this:
form.setAttribute(Qt::WA_DeleteOnClose);
is always included.
I dubugged it but didn't find anything.
Anyway, as it's not causing any issues I 'll leave as is.
Better update twice than none! ):@Panoss You could add
qDebug() << sender();
to your slot - it will print the pointer to the emitter/sender of the signal. Then you will see whether the sender is always the same or different.
-
wrote on 31 Jan 2022, 17:20 last edited by Panoss
I have modified the Signal - Slot mechanism so that makes more sense:
Receiver: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);
Sender:
positionsform.h public slots: void emitSignal(); signals: void valueChanged(int newValue); positionsform.cpp positionsForm::positionsForm(QSqlRelationalTableModel *parentmodel, QWidget *parent) : QDialog(parent), ui(new Ui::positionsForm){ connect(ui->positionsTable->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(emitSignal())); } void positionsForm::emitSignal(){ emit valueChanged(1); }
-
wrote on 31 Jan 2022, 18:59 last edited by mpergand
This code looks strange ...
class positionsForm *form = new class positionsForm(model, this);
The class name doesn't start with a capital letter and looking like a function, confusing.
Why are you using class , obviously positionsForm is a class.and finally:
connect(ui->positionsTable->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(emitSignal()));
since model is defined in ArticlesWindow why not make the connection straight away in that class ?
21/38