Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] QDialog closing
QtWS25 Last Chance

[solved] QDialog closing

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 35.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    p91paul
    wrote on last edited by
    #1

    Hi everybody,
    my problem is that when I click on the OK button on the QDialogButtonBox of my dialog, I have to verify data inserted in the dialog and if there's something wrong, show a messagebox and avoid closing the dialog.
    now I'm handling the accept() slot, and it seems it's impossible to stop closing when clicked on the button.
    Is there a way to avoid window closing?
    thanks
    Paolo

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      Probably you should re-write "QDialog::closeEvent":http://doc.qt.nokia.com/latest/qdialog.html#closeEvent and ignore this event if you wish.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        changsheng230
        wrote on last edited by
        #3

        You can override closeEvent and add your additional test code before accept the closing.

        @void MyDialog::closeEvent(QCloseEvent *event) {
        if (maybeSave()) {
        writeSettings();
        event->accept(); // close window
        } else {
        event->ignore(); // keep window
        }
        }@

        Chang Sheng
        常升

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxim.prishchepa
          wrote on last edited by
          #4

          if you use QPushButton, than you can connect signal to you slot, and then do validation, if your data is valid, then exec QDialog::accept() slot, else - do nothing :)

          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxim.prishchepa
            wrote on last edited by
            #5

            or if you use QDialogButtonBox you can do like that:
            @switch (m_ui.btnBox->buttonRole(btnClicked)){
            case QDialogButtonBox::ResetRole:
            generateDefaultValues();
            updateRows();
            break;
            case QDialogButtonBox::AcceptRole:
            if (isValid()) {
            s_rows = m_rows;
            QDialog::accept();
            }
            break;
            case QDialogButtonBox::RejectRole:
            QDialog::reject();
            break;
            }@

            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              Another approach is ot validate the input data as it changes and only enable the OK button when the data is valid.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p91paul
                wrote on last edited by
                #7

                thanks a lot everybody. I've decided to follow Maxim advice.
                close event is also raised when the user closes the window with the X or via the window manager, and in this case I don't want to save anything.
                bye bye
                Paolo

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved