Results 1 to 3 of 3

Thread: QMdiArea window needs two clicks to close

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QMdiArea window needs two clicks to close

    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
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent *event)
    2. {
    3. mdiArea->closeAllSubWindows();
    4. if (mdiArea->currentSubWindow()) {
    5. event->ignore();
    6. } else {
    7. event->accept();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    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
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QMdiArea window needs two clicks to close

    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():
    Qt Code:
    1. void MdiChild::closeEvent(QCloseEvent *event)
    2. {
    3. if (parentWidget() && testAttribute(Qt::WA_DeleteOnClose)) {
    4. QChildEvent childRemoved(QEvent::ChildRemoved, this);
    5. QApplication::sendEvent(parentWidget(), &childRemoved);
    6. }
    7. event->accept();
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. The following user says thank you to norobro for this useful post:

    epsilon (2nd March 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMdiArea window needs two clicks to close

    Thanks, now I understand what is going on.

Similar Threads

  1. close window signal
    By jano_alex_es in forum Newbie
    Replies: 5
    Last Post: 22nd May 2009, 10:10
  2. how to disable X button to close th window
    By raghvendramisra in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2009, 20:01
  3. How to close parent window from child window?
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2008, 11:40
  4. Weird behavior when readding a window to a QMdiArea
    By seand in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2008, 02:14
  5. close window when click on a label
    By sabeesh in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2007, 07:35

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.