Problems with Open-Source Downloads read and

Button Cancel on Dialog not working

  • Hi all,
    I created a Dialog, i add a button Cancel in ButtonBox but it's not working as X symbol in tittle Dialog.
    I want when click on X symbol in tittle of the Dialog or Cancel button then it closed.
    Please tell me a reason and resolve it.

  • The x in your window will do a quit on your dialog. When you use the Cancel button set to reject it will do a close on your dialog and set the Reject code as return value, but in both cases it should close.
    What doesn't work? Your post is a bit inconclusive.

  • my example:
    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class Dialog;

    class Dialog : public QDialog

    explicit Dialog(QWidget *parent = 0);
    public slots:
    void reject();
    void accept();
    Ui::Dialog *ui;

    #endif // DIALOG_H
    @#include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    ui(new Ui::Dialog)

    delete ui;
    void Dialog::reject(){
    void Dialog::accept(){

    When i click Cancel button, Dialog not closed as expect.

  • Lifetime Qt Champion


    AFAICS your slots are not connected

    And it should rather be

    @this->ui->buttonBox->addButton("CANCEL", QDialogButtonBox::RejectRole);@

  • I try to:
    but it's not closed.

  • Lifetime Qt Champion

    The signal is not emitted like you think it is.

    You have to connect the QDialogButtonBox accepted/rejected signals to your dialog slots accept/reject slots. Close is not a good name since it's a non virtual slot of QWidget and there is already a pair of virtual slots named accept and reject for that.

    Have a look at the "QDialog documentation":

Log in to reply