Close QDialog by code



  • 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!



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



  • Hi,

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



  • You can use close() ?


  • Qt Champions 2016



  • 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???


  • Lifetime Qt Champion

    Hi,

    How are you using your dialog ?



  • 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();
    }
    

  • Lifetime Qt Champion

    Can you share the complete code of your dialog ?



  • 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?


  • Qt Champions 2016

    @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 )



  • 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.


  • Qt Champions 2016

    @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



  • 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. =)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.