Wasabi
7th August 2011, 04:34
I have created a child class of QDialog (NDialog). This class has WA_DeleteOnClose set up in its constructor. This is done because the parent of these dialogs persists for the duration of the entire program and there may be countless dialogs throughout the course of the program, so I'd rather not wait for the end of the program, and thus the destruction of the parent, to destroy all the countless dialogs at once. So I thought it better to simply set WA_DeleteOnClose on each dialog so that, once OK, Cancel or whatnot is pressed, the dialog gets deleted.
This works just fine, as far as I can tell.
However, the class has a static method Error which displays a modal (all others are modeless) dialog with the given error message. In this case, and this case only, WA_DeleteOnClose causes a crash. If I remove this attribute, the error dialog closes just fine, but theoretically won't be deleted until the end of the program.
Should it help, here's the relevant code:
class NDialog : public QDialog
{
Q_OBJECT
QVBoxLayout Layout;//layout box of the dialog
public:
NDialog(QString title,QWidget* parent=0, Qt::WindowFlags f=0) : QDialog(parent,f)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(title);
setLayout(&Layout);
};
void AddWidget(QWidget* widget) //add a widget to the layout
{
Layout.addWidget(widget);
};
static void Error(QString value) //diplays error message
{
NDialog* dlg = new NDialog("ERROR");
dlg->setAttribute(Qt::WA_DeleteOnClose,false);
QLabel msg(value);
dlg->AddWidget(&msg);
QPushButton ok("OK");
connect(&ok,SIGNAL(clicked()),dlg,SLOT(close()));
dlg->AddWidget(&ok);
dlg->exec();
};
};
This is the case where the code works. If I remove the "dlg->setAttribute(Qt::WA_DeleteOnClose,false);" line, the program crashes.
This is the only time I use dlg->exec() as opposed to dlg->show(). Does this have anything to do with it?
This works just fine, as far as I can tell.
However, the class has a static method Error which displays a modal (all others are modeless) dialog with the given error message. In this case, and this case only, WA_DeleteOnClose causes a crash. If I remove this attribute, the error dialog closes just fine, but theoretically won't be deleted until the end of the program.
Should it help, here's the relevant code:
class NDialog : public QDialog
{
Q_OBJECT
QVBoxLayout Layout;//layout box of the dialog
public:
NDialog(QString title,QWidget* parent=0, Qt::WindowFlags f=0) : QDialog(parent,f)
{
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(title);
setLayout(&Layout);
};
void AddWidget(QWidget* widget) //add a widget to the layout
{
Layout.addWidget(widget);
};
static void Error(QString value) //diplays error message
{
NDialog* dlg = new NDialog("ERROR");
dlg->setAttribute(Qt::WA_DeleteOnClose,false);
QLabel msg(value);
dlg->AddWidget(&msg);
QPushButton ok("OK");
connect(&ok,SIGNAL(clicked()),dlg,SLOT(close()));
dlg->AddWidget(&ok);
dlg->exec();
};
};
This is the case where the code works. If I remove the "dlg->setAttribute(Qt::WA_DeleteOnClose,false);" line, the program crashes.
This is the only time I use dlg->exec() as opposed to dlg->show(). Does this have anything to do with it?