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. Don't close the window when clicking "OK" QDialog
Forum Updated to NodeBB v4.3 + New Features

Don't close the window when clicking "OK" QDialog

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 4.4k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    Idodoqdo
    wrote on last edited by
    #1

    Hello everyone, I want the window to not close when the "OK" button is pressed. Here is the code:

    #include "authorizationview.h"
    #include "qmessagebox.h"
    #include "ui_authorizationview.h"
    
    AuthorizationView::AuthorizationView(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::AuthorizationView)
    {
        ui->setupUi(this);
        connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));
    }
    
    AuthorizationView::~AuthorizationView()
    {
        delete ui;
    }
    
    void AuthorizationView::ResultAuthorization(bool result)
    {
        if (result) {
            this->close();
        } else {
            QMessageBox::information(this, "Result Authorization", "Incorrect login or password");
        }
    }
    
    void AuthorizationView::SendInfo()
    {
        emit InfoAuth(qMakePair(ui->login->text(), ui->password->text()));
    }
    
    jsulmJ 1 Reply Last reply
    0
    • I Idodoqdo

      Hello everyone, I want the window to not close when the "OK" button is pressed. Here is the code:

      #include "authorizationview.h"
      #include "qmessagebox.h"
      #include "ui_authorizationview.h"
      
      AuthorizationView::AuthorizationView(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::AuthorizationView)
      {
          ui->setupUi(this);
          connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));
      }
      
      AuthorizationView::~AuthorizationView()
      {
          delete ui;
      }
      
      void AuthorizationView::ResultAuthorization(bool result)
      {
          if (result) {
              this->close();
          } else {
              QMessageBox::information(this, "Result Authorization", "Incorrect login or password");
          }
      }
      
      void AuthorizationView::SendInfo()
      {
          emit InfoAuth(qMakePair(ui->login->text(), ui->password->text()));
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Idodoqdo Override https://doc.qt.io/qt-6/qdialog.html#accept - in your override set the result to Accepted (https://doc.qt.io/qt-6/qdialog.html#setResult), but don't hide the window. Also do not call inherited accepted() method inside of your override.

      But I'm wondering why you want to do this? If I as user click OK button in a dialog I expect it to close or at least to provide some information why it is not possible.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Idodoqdo Override https://doc.qt.io/qt-6/qdialog.html#accept - in your override set the result to Accepted (https://doc.qt.io/qt-6/qdialog.html#setResult), but don't hide the window. Also do not call inherited accepted() method inside of your override.

        But I'm wondering why you want to do this? If I as user click OK button in a dialog I expect it to close or at least to provide some information why it is not possible.

        I Offline
        I Offline
        Idodoqdo
        wrote on last edited by
        #3

        @jsulm I want that when I click "OK", the login and password are sent, the server returns the result and depending on this: either an error message is displayed and the window does not close, or the window closes

        jsulmJ 1 Reply Last reply
        0
        • I Idodoqdo

          @jsulm I want that when I click "OK", the login and password are sent, the server returns the result and depending on this: either an error message is displayed and the window does not close, or the window closes

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Idodoqdo All that can be done with my suggestion

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          I 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Idodoqdo All that can be done with my suggestion

            I Offline
            I Offline
            Idodoqdo
            wrote on last edited by
            #5

            @jsulm this help me

            #include "authorizationview.h"
            #include "qmessagebox.h"
            #include "ui_authorizationview.h"
            
            AuthorizationView::AuthorizationView(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::AuthorizationView)
            {
                ui->setupUi(this);
                connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));
            }
            
            AuthorizationView::~AuthorizationView()
            {
                delete ui;
            }
            
            void AuthorizationView::ResultAuthorization(bool result)
            {
                if (result) {
                    this->close();
                } else {
                    QMessageBox::information(this, "Result Authorization", "Incorrect login or password");
                }
            }
            
            void AuthorizationView::SendInfo()
            {
                emit InfoAuth(qMakePair(ui->login->text(), ui->password->text()));
            }
            
            void AuthorizationView::accept()
            {
                setResult(QDialog::Accepted);
            }
            

            Thx

            JonBJ 1 Reply Last reply
            1
            • I Idodoqdo

              @jsulm this help me

              #include "authorizationview.h"
              #include "qmessagebox.h"
              #include "ui_authorizationview.h"
              
              AuthorizationView::AuthorizationView(QWidget *parent) :
                  QDialog(parent),
                  ui(new Ui::AuthorizationView)
              {
                  ui->setupUi(this);
                  connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));
              }
              
              AuthorizationView::~AuthorizationView()
              {
                  delete ui;
              }
              
              void AuthorizationView::ResultAuthorization(bool result)
              {
                  if (result) {
                      this->close();
                  } else {
                      QMessageBox::information(this, "Result Authorization", "Incorrect login or password");
                  }
              }
              
              void AuthorizationView::SendInfo()
              {
                  emit InfoAuth(qMakePair(ui->login->text(), ui->password->text()));
              }
              
              void AuthorizationView::accept()
              {
                  setResult(QDialog::Accepted);
              }
              

              Thx

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Idodoqdo said in Don't close the window when clicking "OK" QDialog:

              connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));

              I am somewhat surprised this works. What type is ui->buttonBox? It does not help that you are using the old style SIGNAL/SLOT() macros to connect(), which were deprecated a decade ago.

              I 3 Replies Last reply
              0
              • JonBJ JonB

                @Idodoqdo said in Don't close the window when clicking "OK" QDialog:

                connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));

                I am somewhat surprised this works. What type is ui->buttonBox? It does not help that you are using the old style SIGNAL/SLOT() macros to connect(), which were deprecated a decade ago.

                I Offline
                I Offline
                Idodoqdo
                wrote on last edited by
                #7

                @JonB
                491ed691-f779-40d1-999b-27625848d5d6-image.png
                and what is the new way to connect?

                jsulmJ 1 Reply Last reply
                0
                • I Idodoqdo

                  @JonB
                  491ed691-f779-40d1-999b-27625848d5d6-image.png
                  and what is the new way to connect?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Idodoqdo said in Don't close the window when clicking "OK" QDialog:

                  and what is the new way to connect?

                  https://doc.qt.io/qt-6/signalsandslots.html

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Idodoqdo said in Don't close the window when clicking "OK" QDialog:

                    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));

                    I am somewhat surprised this works. What type is ui->buttonBox? It does not help that you are using the old style SIGNAL/SLOT() macros to connect(), which were deprecated a decade ago.

                    I Offline
                    I Offline
                    Idodoqdo
                    wrote on last edited by
                    #9

                    @JonB And what do you not like about this way? Why is he bad? Can you suggest better?

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Idodoqdo said in Don't close the window when clicking "OK" QDialog:

                      connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SendInfo()));

                      I am somewhat surprised this works. What type is ui->buttonBox? It does not help that you are using the old style SIGNAL/SLOT() macros to connect(), which were deprecated a decade ago.

                      I Offline
                      I Offline
                      Idodoqdo
                      wrote on last edited by
                      #10

                      @JonB
                      rewrote like this:

                      connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &AuthorizationView::SendInfo);
                      
                      JonBJ 1 Reply Last reply
                      2
                      • I Idodoqdo

                        @JonB
                        rewrote like this:

                        connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &AuthorizationView::SendInfo);
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Idodoqdo
                        That is much better/clearer/safer. It will also tell you if anything is wrong at compile-time. Basically for your purposes you should continue to do connect()s using this syntax all the time.

                        I 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Idodoqdo
                          That is much better/clearer/safer. It will also tell you if anything is wrong at compile-time. Basically for your purposes you should continue to do connect()s using this syntax all the time.

                          I Offline
                          I Offline
                          Idodoqdo
                          wrote on last edited by
                          #12

                          @JonB said in Don't close the window when clicking "OK" QDialog:

                          That is much better/clearer/safer. It will also tell you if anything is wrong at compile-time. Basically for your purposes you should continue to do connect()s using this syntax all the time.

                          thx

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SimonSchroeder
                            wrote on last edited by
                            #13

                            Does using a QDialogButtonBox within a ui file automatically connect its signals with the surrounding QDialog? I have created a QDialog and a QDialogButtonBox with code instead and there I need to connect the signals myself in order to have the buttons of the QDialogButtonBox do anything at all.

                            JonBJ 1 Reply Last reply
                            0
                            • S SimonSchroeder

                              Does using a QDialogButtonBox within a ui file automatically connect its signals with the surrounding QDialog? I have created a QDialog and a QDialogButtonBox with code instead and there I need to connect the signals myself in order to have the buttons of the QDialogButtonBox do anything at all.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @SimonSchroeder
                              Is this not what the OP's connect(ui->buttonBox, &QDialogButtonBox::accepted, ...) already does?

                              S 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @SimonSchroeder
                                Is this not what the OP's connect(ui->buttonBox, &QDialogButtonBox::accepted, ...) already does?

                                S Offline
                                S Offline
                                SimonSchroeder
                                wrote on last edited by
                                #15

                                @JonB said in Don't close the window when clicking "OK" QDialog:

                                Is this not what the OP's connect(ui->buttonBox, &QDialogButtonBox::accepted, ...) already does?

                                Usually, you would connect to QDialog::accept. So, somewhere I would expect that the dialog is closed on purpose if there is no automatic connection between QDialogButtonBox::accepted and QDialog::accept. Why else would it close?

                                JonBJ 1 Reply Last reply
                                0
                                • S SimonSchroeder

                                  @JonB said in Don't close the window when clicking "OK" QDialog:

                                  Is this not what the OP's connect(ui->buttonBox, &QDialogButtonBox::accepted, ...) already does?

                                  Usually, you would connect to QDialog::accept. So, somewhere I would expect that the dialog is closed on purpose if there is no automatic connection between QDialogButtonBox::accepted and QDialog::accept. Why else would it close?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @SimonSchroeder
                                  Ah, I think I understand you now. I haven't used a QDialogButtonBox. You don't mind the QDialogButtonBox::accepted signal having a slot, but you would expect that to accept the dialog and then the dialog's accepted to do the action, right? Like

                                  connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
                                  connect(this, &QDialog::accepted, this, &AuthorizationView::SendInfo);
                                  

                                  Is this what you had in mind?

                                  But I think there is a wrinkle. The OP's question is "Don't close the window when clicking "OK" QDialog", which the above would do? I think they want the authorization to happen upon accepting/clicking the "OK" button in the dialog button box but before the dialog gets accepted. Then they verify the data and only accept the overall dialog if that has succeeded. I think that is why they do not attach QDialogButtonBox::accepted to QDialog::accept?

                                  S 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @SimonSchroeder
                                    Ah, I think I understand you now. I haven't used a QDialogButtonBox. You don't mind the QDialogButtonBox::accepted signal having a slot, but you would expect that to accept the dialog and then the dialog's accepted to do the action, right? Like

                                    connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
                                    connect(this, &QDialog::accepted, this, &AuthorizationView::SendInfo);
                                    

                                    Is this what you had in mind?

                                    But I think there is a wrinkle. The OP's question is "Don't close the window when clicking "OK" QDialog", which the above would do? I think they want the authorization to happen upon accepting/clicking the "OK" button in the dialog button box but before the dialog gets accepted. Then they verify the data and only accept the overall dialog if that has succeeded. I think that is why they do not attach QDialogButtonBox::accepted to QDialog::accept?

                                    S Offline
                                    S Offline
                                    SimonSchroeder
                                    wrote on last edited by
                                    #17

                                    @JonB I'm not saying you should connect those (certainly not if you don't want the dialog to close upon clicking 'OK'). I was saying that either there is some auto-connect because of the ui-file (which I don't know) or somewhere in the code the dialog is closed on purpose. If the OP thinks he's not closing the dialog himself on purpose, then the auto-connect would be one of my guesses to check.

                                    JonBJ 1 Reply Last reply
                                    0
                                    • S SimonSchroeder

                                      @JonB I'm not saying you should connect those (certainly not if you don't want the dialog to close upon clicking 'OK'). I was saying that either there is some auto-connect because of the ui-file (which I don't know) or somewhere in the code the dialog is closed on purpose. If the OP thinks he's not closing the dialog himself on purpose, then the auto-connect would be one of my guesses to check.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #18

                                      @SimonSchroeder said in Don't close the window when clicking "OK" QDialog:

                                      or somewhere in the code the dialog is closed on purpose. If the OP thinks he's not closing the dialog himself on purpose

                                      In the OP's void AuthorizationView::ResultAuthorization(bool result) he has

                                          if (result) {
                                              this->close();
                                      

                                      His code emits a signal with the name & password, the authorizer presumably calls above method with success/failure parameter for authorization. If success it closes the dialog, but if failure it puts up a message box but stays inside the dialog, presumably for the user to try again. That is why the button box's "OK"/accepted should not be connected directly and unconditionally to the dialog's accepted.

                                      1 Reply Last reply
                                      1

                                      • Login

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