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.
    @this->ui->buttonBox->addButton("CANCEL",QDialogButtonBox::RejectRole);@
    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.
    Thanks.



  • 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:
    dialog.h
    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

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

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

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    this->ui->buttonBox->addButton("CANCEL",QDialogButtonBox.RejectRole);
    }

    Dialog::~Dialog()
    {
    delete ui;
    }
    void Dialog::reject(){
    QApplication::quit();
    }
    void Dialog::accept(){

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


  • Lifetime Qt Champion

    Hi,

    AFAICS your slots are not connected

    And it should rather be

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



  • I try to:
    @connect(this,SIGNAL(rejected()),this,SLOT(close()));@
    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":http://qt-project.org/doc/qt-4.8/qdialog.html


Log in to reply
 

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