c++ - How to return a value to a previous window in Qt?

Asked

Viewed 563 times

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.

    1. The destructor is not called in your code (where is the delete corresponding to the new?); 2) The way your code works is the proper way to do this, there is no reason why it does not work, considering that you are copying the width value to an integer in the current form (for it to remain after use of the function a member is required and not a local variable).
  • 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, 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.

  • 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.

  • 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.

  • 1

    The show does not "lock" the window open for the user to type. Use exec 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 (using delete on the pointer previously allocated with new). You can also check the return of exec to see if the user canceled the edition. Take a look at the examples of MsgBox. I’m running out of time to prepare an answer, but the idea is there.

  • 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.

Show 2 more comments

1 answer

1


My problem was simple resolution. I added the line tamVeic->exec(); after the call from tamVeic->show();, which allowed the value to be returned to the main window. I also had to change the type (inheritance) of QWidget for QDialog to be able to use the method exec();. This method hangs the execution of the caller until the window is closed by the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.