Hi Satmosc,
What I meant by that was..
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
delete m_testWidget;
}
void MainWindow::on_showButton_clicked()
{
m_testWidget = new QWidget();
m_testWidget->setAttribute(Qt::WA_DeleteOnClose);
m_testWidget->show();
}
@
In the above example, when we click the button a new window is shown, and as you can see, the window is created without a parent.
The code would give a segfault in trying to delete the m_testWidget twice. When we close the window manually, it's deleted, but we are also deleting it in our destructor, so, it causes a crash.
Of course, if you have a parent for a widget, then it is not a window by default, but can be made into one using the setWindowFlags(Qt::Window) as you have pointed out.
I'm not able to replicate a case where the Qt::WA_DeleteOnClose conflicts with Qt's automatic destruction of objects using the parent/child. But what I meant about the main() function is:
Don't do this:
@
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setAttribute(Qt::WA_DeleteOnClose);
w.show();
return a.exec();
}
@
Because, in this case, we would get a seg fault at the end of execution, because the main window is deleted on close, and at the end of main() which returns after the close of the main window, the variable "w" allocated on the stack will be tried to be removed causing multiple deletions.
If I find some more relevant documentation, I will add it to this thread.
[EDIT: Thank you Volker for the additional info. As mentioned above, you can use both the automatic memory management using parent/child and Qt::WA_DeleteOnClose together safely. So this is okay..
@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_showButton_clicked()
{
m_testWidget = new QWidget(this);
m_testWidget->setWindowFlags(Qt::Window);
m_testWidget->setAttribute(Qt::WA_DeleteOnClose);
m_testWidget->show();
}
@
)