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

Dialog box with Proceed and Cancel buttons



  • I have created my own dialog box.

    class CWarningDialog : public QDialog
    

    I have also created a couple of buttons, ok and cancel, via code

    QPushButton	*BUTTONProcess;   //This is like a Ok button
    QPushButton	*BUTTONCancel;
    

    Under MFC, I gave the Proceed button the ID of IDOK and Cancel is IDCANCEL.
    So, when Proceed or Cancel is pressed, I get an integer returned by the DoModal() member function.

    What is the Qt equivalent code to this?

    //MFC version
    int returnVal;
    returnVal=adlg_WarningDialog.DoModal();
    if(returnVal==IDCANCEL)
    {
    }
    
    //My Qt equivalent code
    int returnVal;
    returnVal=adlg_WarningDialog.exec();
    if(returnVal)
    {
    }
    

  • Lifetime Qt Champion



  • I got it working.

    void CWarningDialog::OnButtonProceed()
    {
    	accept();
    }
    
    void CWarningDialog::OnButtonCancel()
    {
    	reject();
    }
    

    and in my main window code

    int returnVal;
    returnVal=adlg_WarningDialog.exec();
    if(returnVal==QDialog::Rejected)
    {
    }
    

  • Lifetime Qt Champion

    These two custom slots are overkill. You can directly connect your buttons to the appropriate accept/reject slots.


Log in to reply