Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Best way QDialog should communicate with MainWindow?



  • Hello,

    I have a QDialog with 2 buttons. One button performs an operation and the other is a cancel button. The button the performs an operation, emits a signal then closes the dialog.

    I want to know which of the two buttons was clicked when the QDialog closes and control returns to MainWindow. I read in other posts that the QDialog should not be messing with variable created in its parent, MainWindow.

    Thank you



  • Hi, I believe the Cancel button returns QDialog::Rejected; I recommend that you have your other button do its stuff then call accept(); Thus returning either QDialog::Accepted or QDialog::Rejected.

    So in your code you could do as

    result = dialog.exec();
    

    Then result will either be Accepted or Rejected depending on the button pressed.


  • Moderators

    QDialog::Accept and QDialog::Reject are the built-in result types, as @Chrisw01 mentioned.
    If you want more customized options you can use any integer value with done(). Create a public enum in your dialog class with the options and then connect your buttons to slots that call done() with that enum values.
    A user of your dialog can then call exec() and inspect the return value by comparing it to the enum values.


Log in to reply