PDA

View Full Version : [SOLVED] detecting when a mdi window is closed



elcuco
9th September 2006, 16:04
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:



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-programming-2/t-how-to-hide-window-when-close-button-pressed-3144.html

fullmetalcoder
9th September 2006, 16:11
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).

elcuco
9th September 2006, 16:39
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.



void qmdiWorkspace::addTab( QWidget *widget, QString name )
{
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();
}

macbeth
9th September 2006, 19:22
what about reimplementing QWidget::closeEvent() ? Maybe you could call destroy() function in your closeEvent... Havn't tried it, it's just a thought...

elcuco
9th September 2006, 19:49
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.

jacek
9th September 2006, 20:04
The idea is to stick standard QWidgets, not modified ones into the work space.
You can always use an event filter.

jacek
9th September 2006, 20:09
The docs say:
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:
QWidget *frame = workspace->addWindow( widget );
frame->setAttribute( Qt::WA_DeleteOnClose );

elcuco
9th September 2006, 21:37
Almost... now I got it:



#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"?)

jacek
9th September 2006, 21:50
The problem was th second parameter to setAttribute.
IMO the problem was in order of statements.