Important: Please read the Qt Code of Conduct -

How to realize the cancel operation of setting/option dialog?

  • Every app has it's setting/option dialog. In this dialog, it has many widgets, such as, pushbutton, groupbox, lineedit, combobox,etc.

    If ths user update these widgets' contents in setting/option dialog, but click cancel pushbutton to undo the previous error/wrong operations.

    The setting/option dialog should keep the original state when next dialog 's opening.

    My solution is using extra variables to save these widgets' contents in setting/option dialog, such as, bool variable to save QGroupBoxint variable to save QComboBox, QString variable to save QLineEdit/QTextEdit,

    Just like,

    class OptionDialog : public QDialog {
    QLineEdit*     m_hostLineEdit;
    QLineEdit*     m_portLineEdit;
    QLineEdit*     m_userLineEdit;
    QLineEdit*     m_pwdLineEdit;
    QString        m_host; // to save m_hostLineEdit
    QString        m_port; // to save m_portLineEdit
    QString        m_username; // to save m_userLineEdit
    QString        m_password; // to save m_pwdLineEdit
    void ok() // when clicked the ok pushbutton
        /* save */
        m_host = m_hostLineEdit->text();
        m_port = m_portLineEdit->text();
        m_username = m_userLineEdit->text();
        m_password = m_pwdLineEdit->text();
    void cancel()  // when clicked the cancel pushbutton
        // undo
    } // class OptionDialog : public QDialog

    Is there anything wrong with my solution?

    If a bad solution, could you give me any advice? Thanks a lot in advance.

  • @Limer
    I think you will find that is really the only way to do it.
    Have local values in the dialog and only update the real ones when the user clicks OK. Or, only transfer the value in the controls when the user clicks OK.

Log in to reply