sark9012
sark9012

Reputation: 5737

QT dialog close and delete

I have a MainWindow and Type class.

A button in the MainWindow sends a signal to a slot with this code:

dialog = new QDialog(this);

Ui_type typeui;
typeui.setupUi(dialog);

dialog->show();

The dialog then shows. When a button is clicked on the dialog, I want to close the dialog and delete it.

I don't understand how to refer to the dialog from the dialog itself.

Any help would be appreciated. Thanks.

Upvotes: 2

Views: 25354

Answers (3)

Jakub
Jakub

Reputation: 573

The simple way to get input from a modal dialog is QDialog::exec(). This may handle everything you need.

Upvotes: 1

zero
zero

Reputation: 193

First the close button is at the dialog window right, then most easy way to do it, is create a button, and connect the close() function to response the click() signal. like:

Dialog::Dialog(){
   // other code
   QPushButton *closeButton = new QPushButton(tr("Close"));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
   // other code
}

Under the Qt/examples/dialog projects are good reference for your question. check it out.

Upvotes: 5

Murat Şeker
Murat Şeker

Reputation: 1711

You can set Qt::WA_DeleteOnClose attribute on your dialog. This will ensure that the dialog gets deleted whenever it is closed.

Then call close() method in the dialog when your button is clicked.

dialog = new QDialog(this);

Ui_type typeui;
typeui.setupUi(dialog);

dialog->setAttribute(Qt::WA_DeleteOnClose);

dialog->show();

Refer to the documentation for details :

QWidget::setAttribute ( Qt::WidgetAttribute attribute, bool on = true )

Qt::WidgetAttribute

Upvotes: 17

Related Questions