Results 1 to 9 of 9

Thread: [SOLVED] detecting when a mdi window is closed

  1. #1
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [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:

    Qt Code:
    1. connect( widget, SIGNAL(destroyed(QObject*)), this, SLOT(windowDeleted2(QObject*)));
    2.  
    3. ...
    4. void qmdiWorkspace::windowDeleted( QObject *o )
    5. {
    6. int windowNumber = workspace->windowList().lastIndexOf(dynamic_cast<QWidget*>(o));
    7. if (windowNumber!=-1)
    8. tabBar->removeTab( windowNumber );
    9. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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).
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. void qmdiWorkspace::addTab( QWidget *widget, QString name )
    2. {
    3. if (!workspace)
    4. return;
    5.  
    6. qmdiClient *client = dynamic_cast<qmdiClient*>(widget);
    7. if (client)
    8. client->mdiServer = this;
    9.  
    10. connect( widget, SIGNAL(destroyed(QObject*)), this, SLOT(windowDeleted(QObject*)));
    11. widget->setAttribute(Qt::WA_DeleteOnClose, true);
    12. workspace->addWindow( widget );
    13. tabBar->addTab( name );
    14. widget->show();
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2006
    Location
    Poprad/Prague
    Posts
    33
    Thanks
    4
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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...

  5. The following user says thank you to macbeth for this useful post:

    elcuco (9th September 2006)

  6. #5
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: detecting when a mdi window is closed

    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:
    Qt Code:
    1. QWidget *frame = workspace->addWindow( widget );
    2. frame->setAttribute( Qt::WA_DeleteOnClose );
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Jan 2006
    Posts
    371
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: detecting when a mdi window is closed

    Almost... now I got it:

    Qt Code:
    1. #if 0
    2. QWidget *frame = workspace->addWindow( widget );
    3. frame->setAttribute( Qt::WA_DeleteOnClose );
    4. #else
    5. workspace->addWindow( widget );
    6. widget->setAttribute( Qt::WA_DeleteOnClose );
    7. #endif
    To copy to clipboard, switch view to plain text mode 

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

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. Replies: 3
    Last Post: 23rd July 2006, 18:02
  2. animate a window
    By iGoo in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2006, 10:46
  3. Move window in Clone Mode
    By ultrabrite in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2006, 18:22
  4. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 10:21
  5. delete sub windows when they are closed?
    By Dark_Tower in forum Newbie
    Replies: 3
    Last Post: 21st March 2006, 18:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.