QDialog to QDialog communication



  • Hi,

    I want info to be passed from one QDialog to another. Should this be handled by signal/slot or is there another mechanism to handle the task?


  • Qt Champions 2016

    Hi
    While you can use plain functions, signals & slots are far Superior as you hide the details
    from rest of the program.

    So you simple define a set of signals and slot that defines what they want to "say" to each other
    and then hook it up.



  • Just need a clarification

            close();
            Information* info = retrieveInfo();
            InformationDialog infoDialog;
            infoDialog.setModal(true);
            connect(this, SIGNAL(InfoChanged(Information*)), &infoDialog, SLOT(on_Info_Changed(Information*)));
            emit on_Info_Changed(Information);
            infoDialog.exec();
    

    basically I want the first dialog to search for information and then close and display the information on the 2nd dialog. The above code works but I just don't get how "this" in connect() still exists after the call to close();

    Will this code cause problems?



  • @WhatIf said:

    close

    Perhaps because the first dialog is closed but not deleted from memory?
    You could set the Qt::WA_DeleteOnClose flag and see if it still works.



  • When I set the Qt::WA_DeleteOnClose flag, the program crashes when I click cancel in the 2nd dialog, which calls close().

    Also, it the order of

    emit on_Info_Changed(Information);
    infoDialog.exec();
    

    correct?


  • Qt Champions 2016

    @WhatIf
    Hi
    close dont call delete (unless flag set) so that is why it works.
    the "this" is still valid and we block at infoDialog.exec() so
    its not deleted behind our back.

    When i read your question i assumed you would have dialogs open at the same time. hence signal would be the best. But it more like in sequence i see

    it could be a bit cleaner if you can let mainwindow handle it and we then get rid of "one dialog open the other one" as its best to avoid if possible.
    Its not wrong as such but not optimal as Qt::WA_DeleteOnClose can crash the
    code.

    what about something like ?

    void mainwindow::Search() {
      SeachDia SD;
      InformationDialog infoDialog () ;
      if ( SD.exec() == Accepted) {
        Information* info = SD.retrieveInfo();
        infoDialog.setSearchData(info);
        infoDialog.exec();
      }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.