Details
-
Bug
-
Resolution: Out of scope
-
P3: Somewhat important
-
4.3.1
-
None
-
Any
Description
A code segment in MainWindow::open():
MdiChild *child = createMdiChild();
if (child->loadFile(fileName))
else
{ child->close(); }Problem:
MainWindow::createMdiChild() adds an anonymous object "subWindow" of QMdiSubWindow to the object of QMdiArea. The variable child points to a object "child" of MdiChild, and "child" is the widget of the anonymous object "subWindow".
When child->loadFile() return false, the child->close() is called, which will result in that child is destructured because it is set (Qt::WA_DeleteOnClose). Thus, the "child" object does not exist any more. Because the widget of the "subWindow" is the "child" object, the "child" notifies its parent to remove it.
--Now, the widget of "subWindow " is 0(NULL). – //Danger!
The object "subWindow" has no widget in the object of QMdiArea is a dangerous state.
Later, users can use MDI to open any file. If an user open a file, then an error occurs in the following code:
QMdiSubWindow *existing = findMdiChild(fileName);
MainWindow::findMdiChild() will search every widget pointed by objects of QMdiSubWindow in QMdiArea.
The "subWindow" is accessed to return its pointed widget which is 0 now.
A code segment in MainWindow::findMdiChild():
MdiChild *mdiChild = qobject_cast<MdiChild *>(window->widget());
if (mdiChild->currentFile() == canonicalFilePath) -----------------------------------//error is catched by QString::==!!!!!!!!!!!!!
return window;
Solution:
The most quick solution substitues "child->close();" by "delete child->parentWidget();"
the key point is that make all objects in QMdiArea are pointing to not 0.