PDA

View Full Version : QMdiArea window needs two clicks to close



epsilon
2nd March 2010, 02:38
Hi group,
I have been experimenting with the MDI example program. I wanted to change the child Mdi windows from the standard QTextedit to something else (an activeX control eventually...) but I am having some trouble with the basics. When I change the MdiChild object inheritance from QTextEdit to QMdiSubWindow,
e.g.
class MdiChild : public QMdiSubWindow //QTextEdit

everything works fine until I close the app (via the topright mainwindow x icon). First click all the child windows disappear (tested with up to 3 visible) but the app does not close. Clicking again and the app exits normally.

So I stripped out almost everything to make a very lightweight project but changing the above class declaration to QTextEdit (or QLabel) makes everything normally and flipping back to QMdiSubWindow causes the problem again.

It turns out that in

void MainWindow::closeEvent(QCloseEvent *event)
{
mdiArea->closeAllSubWindows();
if (mdiArea->currentSubWindow()) {
event->ignore();
} else {
event->accept();
}
}
currentSubWindow is returning True in one case but False (as it should) when the declaration is QTextEdit. Anybody know why?

I have attached the stripped down project that reproduces the problem. Any help would be appreciated.

Thanks,
epsilon

norobro
2nd March 2010, 05:55
The problem is that you are not properly reimplementing closeEvent() in your MdiChild class when you are deriving from QMdiSubWindow. Your MdiChild::closeEvent() is overriding QMdiSubWindow::closeEvent(). Obviously, when you are deriving from QTextEdit it doesn't. To make it work you need to add the following to MdiChild::closeEvent():

void MdiChild::closeEvent(QCloseEvent *event)
{
if (parentWidget() && testAttribute(Qt::WA_DeleteOnClose)) {
QChildEvent childRemoved(QEvent::ChildRemoved, this);
QApplication::sendEvent(parentWidget(), &childRemoved);
}
event->accept();
}

Really, there is no need to reimplement closeEvent() unless you are doing more than accepting the event. Your stripped down code should work without it derived from either class.

epsilon
2nd March 2010, 07:08
Thanks, now I understand what is going on.