Important: Please read the Qt Code of Conduct -

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?

  • Lifetime Qt Champion

    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

            Information* info = retrieveInfo();
            InformationDialog infoDialog;
            connect(this, SIGNAL(InfoChanged(Information*)), &infoDialog, SLOT(on_Info_Changed(Information*)));
            emit on_Info_Changed(Information);

    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:


    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);


  • Lifetime Qt Champion

    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

    what about something like ?

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

Log in to reply