2
Hello, I am developing a project where I will have many calls from new windows and need to recover a value from these windows to the main window (Mainwindow). The problem is that it becomes impossible to get the value returned because, when closing the new window, the value is lost with its destructor and is not returned to the main window.
EDIT2:
My code from the new window where I return a value (tamveicdialog.cpp):
int TamVeicDialog::getLargura(){
return this->largura;
}
void TamVeicDialog::on_ButtonSalvar_clicked(){
this->close();
}
Call from my new window in the main window:
void mainwindow::on_ButtonTamVeic_clicked(){
TamVeicDialog *tamVeic = new TamVeicDialog();
tamVeic->setFixedSize(696, 368);
tamVeic->reset(); // seta alguns valores de atributos da nova janela //
tamVeic->show();
int larg = tamVeic->getLargura();
qDebug() << "Largura retornada: " << larg;
}
The idea is to click save in the Tamveic window, it returns the width value to Mainwindow. Would anyone know how to return this value to my Mainwindow? Thank you already.
Note: As much as my window has 'dialog' in its statement, it is of type Qwidget, I think this question may have caused problems to understand my problem.
– Bruno Bermann
In my new window, the user performs an operation and clicks on a "Save" button, when clicking, the window closes, I believe the destructor is called the moment the window closes, and this way, I cannot return a value to the previous window. I did not understand what you meant by a 'member'. Thank you for the reply.
– Yuri Pires
Yuri, the way your code is structured the object will not be released from memory (and this causes a memory leak), because if your memory were released (and the destructor called) you would not be able to access "tamVeic->getLargure();" This would generate an error at runtime. You must delete the object that was instantiated with new obligatorily and please post the code of "Tamveicdialog" so that maybe we can help you.
– Bruno Bermann
I edited my post, Tamveic’s code is too big, so it would be unfeasible to post it completely, sorry. But however what matters in this case is the return of the width variable to the Parent window (Mainwindow). The intention was that when clicking the save button of the new window, it would return the modified value of width to the previous screen. I hope I’ve been able to explain a little better.
– Yuri Pires
Thanks for the answer. Would you have any examples using Signals and slots or this modal? I have researched in some forums and most of the examples are from Mainwindow to daughter windows.
– Yuri Pires
The
show
does not "lock" the window open for the user to type. Useexec
after the show, so the window stays in its own Loop until the user closes it. Then, just take the desired field value and delete the instance manually (usingdelete
on the pointer previously allocated withnew
). You can also check the return ofexec
to see if the user canceled the edition. Take a look at the examples ofMsgBox
. I’m running out of time to prepare an answer, but the idea is there.– Luiz Vieira
I forgot to mention the details that even with 'dialog on name', my window class inherits from a Qwidget, I think this may have caused problems to understand my question. I will create a Qdialog and try to use exec() the way you spoke. Thank you @Luizvieira for the reply.
– Yuri Pires