Search code examples
qtqdialogisenabled

How to enable a button on QDialog1 when QDialog2 closes


I have a QDialog called Dialog1 with two buttons btnDialog2 and btnDialog3.

On clicking btnDialog2 and btnDialog3 I have have the following code run:

void Dialog1::on_btnDialog2_clicked()
{
    ui->btnDialog2->setEnabled(false);

    d2 = new AltDialog(this);
    d2->setWindowTitle("Dialog 2");
    d2->show();
}

void StockItems::on_btnDialog3_clicked()
{
    ui->btnDialog3->setEnabled(false);

    d3= new AltDialog(this);
    d3->setWindowTitle("Dialog 3");
    d3->show();
}

As expected, if dialog2 or dialog3 is opened, their respective buttons one dialog1 will be disabled.

I want to re-enable the buttons on dialog1 upon their respective dialogs closing.

Note:

The Main Dialog has the following two private variables to represent each dialog:

AltDialog *d2, *d3;

Any help on accomplishing my goal would be much appreciated!


Solution

  • Your application needs to get notification from the specific dialog if it was closed.

    You can create new slots in your Main Dialog to receive signals from the d2 and d3 dialogs; and connect, for example, void QDialog::finished(int result) signal to be caught by new created slots:

    d2 = new AltDialog(this);
    connect(d2, &QDialog::finished, this, &YourMainDialog::d2Finished);
    d2->setWindowTitle("Dialog 2");
    
    ...
    
    void YourMainDialog::d2Finished(int result)
    {
      ui->btnDialog2->setEnabled(true);
    }
    

    P.S. You need to aware that finished signal "is emitted when the dialog's result code has been set, either by the user or by calling done(), accept(), or reject()." Also it doesn't look right that you are creating new AltDialog object on every button press. It seems it should be moved to "initialization" methods of your Main Dialog along with signals connections.