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. Close QDialog by code
Qt 6.11 is out! See what's new in the release blog

Close QDialog by code

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 9.1k Views 3 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.
  • SikarjanS Offline
    SikarjanS Offline
    Sikarjan
    wrote on last edited by
    #1

    Hi,

    I am blind at the moment. I would like to close a QDialog by code. In the dialog you are able to select a file path. In case the returned path is empty (user canceled the QFileDialog) the QDialog shall be closed as well. I used a standard Qt-Designer-Formular-Class with okay and cancel buttons in a ButtonBox. Can I some how trigger the cancel button when the returned string is empty?

    Thanks!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      Can you check the length of the string and call dialog.quit or close function ? This should close the dialog.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      4
      • SikarjanS Offline
        SikarjanS Offline
        Sikarjan
        wrote on last edited by
        #3

        Hi,

        sorry but there is no this->quit() or ui->quit() or Dilog.quit();

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
          wrote on last edited by
          #4

          You can use close() ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          https://www.pthinks.com

          1 Reply Last reply
          7
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Maybe also
            http://doc.qt.io/qt-5/qdialog.html#reject

            1 Reply Last reply
            2
            • SikarjanS Offline
              SikarjanS Offline
              Sikarjan
              wrote on last edited by Sikarjan
              #6

              Thanks for all the hints so far but none worked.

              void InsertImageDialog::on_pushButton_clicked()
              {
                  QString imgSrc = QFileDialog::getOpenFileName(this, tr("Select image"), mPath, tr("Image Files (*.png *.jpg *.gif);; All files (*)"));
              
                  if(imgSrc.isEmpty()){
                      this->reject(); // I also tried ui and ui->buttonBox
                      return;
                  }
              }
              

              This is not working. The QDialig is not closed. I don't understand it. According to the documentation it should work.

              void QDialog::rejected()

              This signal is emitted when the dialog has been rejected either by the user or by calling reject() or done() with the QDialog::Rejected argument.

              When the QFileDialog is canceled the imgSrc string is empty, I double checked that. The code inside the statement should be executed. What could I possibly do wrong???

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                How are you using your dialog ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • SikarjanS Offline
                  SikarjanS Offline
                  Sikarjan
                  wrote on last edited by
                  #8

                  Hi,

                  I call the dialog as a modal dialog:

                  void MainWindow::on_actionImage_triggered()
                  {
                      CodeEditor *mEditor = checkForEditor();
                      InsertImageDialog insertImage;
                      insertImage.setModal(true);
                      connect(&insertImage, SIGNAL(insertImage(QString,int,int,QString,QString)), mEditor, SLOT(insertImage(QString,int,int,QString,QString)));
                      insertImage.exec();
                  }
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Can you share the complete code of your dialog ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • SikarjanS Offline
                      SikarjanS Offline
                      Sikarjan
                      wrote on last edited by
                      #10

                      Hi,

                      while trying to build minimal example I found the issue but not the solution. I am calling the on_pushButton_clicked() at the end of the constructor because selecting a file is the first thing the user needs to do. I guess the constructor is not finished while the user picks a file and therefore the reject is not available at the point.

                      How can I call the pushButton event directly after the dialog is opened?

                      mrjjM 1 Reply Last reply
                      0
                      • SikarjanS Sikarjan

                        Hi,

                        while trying to build minimal example I found the issue but not the solution. I am calling the on_pushButton_clicked() at the end of the constructor because selecting a file is the first thing the user needs to do. I guess the constructor is not finished while the user picks a file and therefore the reject is not available at the point.

                        How can I call the pushButton event directly after the dialog is opened?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Sikarjan said in Close QDialog by code:

                        How can I call the pushButton event directly after the dialog is opened?

                        What about letting the user press the Select Image button ?

                        If you really want to popup automatically, you can use a timer.
                        QTimer::singleShot(5000, this, SLOT(your_slot_function()));

                        there is also
                        showEvent(QShowEvent * event )
                        ( you must implement in InsertImageDialog )

                        1 Reply Last reply
                        1
                        • SikarjanS Offline
                          SikarjanS Offline
                          Sikarjan
                          wrote on last edited by
                          #12

                          Thanks @mrjj the QTimer solution works but it is not very nice. First the Dialog opens and then with some delay the select file menu. There is always a delay, even if you put the time to 0 ms. Also the select file window is positioned oddly. It is not centered over the dialog.

                          I did not try the other solution suggested.

                          mrjjM 1 Reply Last reply
                          0
                          • SikarjanS Sikarjan

                            Thanks @mrjj the QTimer solution works but it is not very nice. First the Dialog opens and then with some delay the select file menu. There is always a delay, even if you put the time to 0 ms. Also the select file window is positioned oddly. It is not centered over the dialog.

                            I did not try the other solution suggested.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @Sikarjan
                            Well the whole idea of popping the image dialog over
                            another dialog is a bit unusual so that might also be the reason its not working super well. :)

                            showEvent might be triggered multiple times so make sure u handle that.

                            I wonder if u just can do it like

                            QString imgSrc = QFileDialog::getOpenFileName(this, tr("Select image"), mPath, tr("Image Files (*.png *.jpg .gif);; All files ()"));
                            if( ! imgSrc.isEmpty()) { // if NOT empty
                            TheOTherdialog dia;
                            if ( dia.exec() ) {
                            // do code
                            }
                            }

                            So ask image name.
                            If selected something
                            pop the dialog and do X
                            if ok to dialog, process

                            1 Reply Last reply
                            2
                            • SikarjanS Offline
                              SikarjanS Offline
                              Sikarjan
                              wrote on last edited by
                              #14

                              I see. I should open the dialog only when I have a valid file selected. Sure, that would be possible. It would be a bit more code since I have to do the select and check twice, once in the mainWindow and once in the dialog.

                              Thanks for the idea, did not think about that. =)

                              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