[SOLVED] detecting when a mdi window is closed
Hi,
My application has a QWorkspace, and I am adding to it a tab bar, to look like a QTabWidget. I am trying to detect when a window is closed, to update the window list. I am using this code:
Code:
connect( widget,
SIGNAL(destroyed
(QObject*)),
this,
SLOT(windowDeleted2
(QObject*)));
...
void qmdiWorkspace
::windowDeleted( QObject *o
) {
int windowNumber = workspace->windowList().lastIndexOf(dynamic_cast<QWidget*>(o));
if (windowNumber!=-1)
tabBar->removeTab( windowNumber );
}
Now that function is never called. It seems that pressing the X on the windows, hides then and does not close them. Does anyone have an idea about a possible fix? another implementation of this feature?
I also read this thread, but I found no solution to my problem.
http://www.qtcentre.org/forum/f-qt-p...ssed-3144.html
Re: detecting when a mdi window is closed
You should set the Qt::WA_DeleteOnClose attribute to true when "registering" the widget into the workspace... (That's the way it works in Edyuk).
Re: detecting when a mdi window is closed
I followed your advice, and this is the code I ended with. Still, when I click the X on the windows, the signal is still not emmited.
Code:
{
if (!workspace)
return;
qmdiClient *client = dynamic_cast<qmdiClient*>(widget);
if (client)
client->mdiServer = this;
connect( widget,
SIGNAL(destroyed
(QObject*)),
this,
SLOT(windowDeleted
(QObject*)));
widget->setAttribute(Qt::WA_DeleteOnClose, true);
workspace->addWindow( widget );
tabBar->addTab( name );
widget->show();
}
Re: detecting when a mdi window is closed
what about reimplementing QWidget::closeEvent() ? Maybe you could call destroy() function in your closeEvent... Havn't tried it, it's just a thought...
Re: detecting when a mdi window is closed
Quote:
Originally Posted by macbeth
what about reimplementing QWidget::closeEvent() ? Maybe you could call destroy() function in your closeEvent... Havn't tried it, it's just a thought...
The idea is to stick standard QWidgets, not modified ones into the work space.
Re: detecting when a mdi window is closed
Quote:
Originally Posted by elcuco
The idea is to stick standard QWidgets, not modified ones into the work space.
You can always use an event filter.
Re: detecting when a mdi window is closed
The docs say:
Quote:
QWidget * QWorkspace::addWindow ( QWidget * w, Qt::WFlags flags = 0 )
Adds widget w as new sub window to the workspace. If flags are non-zero, they will override the flags set on the widget.
Returns the widget used for the window frame.
To remove the widget w from the workspace, simply call setParent() with the new parent (or 0 to make it a stand-alone window).
Try:
Code:
QWidget *frame
= workspace
->addWindow
( widget
);
frame->setAttribute( Qt::WA_DeleteOnClose );
Re: detecting when a mdi window is closed
Almost... now I got it:
Code:
#if 0
QWidget *frame
= workspace
->addWindow
( widget
);
frame->setAttribute( Qt::WA_DeleteOnClose );
#else
workspace->addWindow( widget );
widget->setAttribute( Qt::WA_DeleteOnClose );
#endif
The first part of the code, is what you suggest. That does not work. The second one (on the else) does work.
The problem was th second parameter to setAttribute. THANKS!
(how do I change the thread name to "solved"?)
Re: detecting when a mdi window is closed
Quote:
Originally Posted by elcuco
The problem was th second parameter to setAttribute.
IMO the problem was in order of statements.