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_OBJECTpublic:
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. -
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