Results 1 to 8 of 8

Thread: midi child does not close when I call close()

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default midi child does not close when I call close()

    Hi,

    I have a Mdi child with a close button. I connected the signal clicked() of the button to the slot close() of the Mdi child.

    But when I click close the button disappears and the window does not close!

    The MdiChild is based on a QMainWindow.

    Any idea why?!!!

    Thanks.
    Carlos
    Last edited by qlands; 28th July 2011 at 10:25.

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: midi child does not close when I call close()

    show the code of the connection. you probably connected wrong objects.

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: midi child does not close when I call close()

    Hi,

    Qt Code:
    1. #include "documentwindow.h"
    2. #include "ui_documentwindow.h"
    3.  
    4. documentWindow::documentWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::documentWindow)
    7. {
    8. ui->setupUi(this);
    9. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
    10. }
    11.  
    12. documentWindow::~documentWindow()
    13. {
    14. }
    To copy to clipboard, switch view to plain text mode 

    So when I click in the pushButton the window does not close!

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: midi child does not close when I call close()

    Hi,

    the code looks good to me. the problem might be related to the mdi-stuff you are doing. can you show the mdi-code?

    Felix

  5. #5
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: midi child does not close when I call close()

    Hi,

    the MdiChild is declared as:
    Qt Code:
    1. #include <QMainWindow>
    2. #include <impgenmaint.h>
    3.  
    4. namespace Ui {
    5. class documentWindow;
    6. }
    7.  
    8. class documentWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit documentWindow(QWidget *parent = 0);
    14. ~documentWindow();
    15.  
    16. private:
    17. Ui::documentWindow *ui;
    18. };
    To copy to clipboard, switch view to plain text mode 

    The implementation is the same I previously posted.

    I copied the QT mdi example, thus the code the created the mdi child is:
    Qt Code:
    1. documentWindow *MainWindow::createMdiChild()
    2. {
    3. documentWindow *child = new documentWindow;
    4. mdiArea->addSubWindow(child);
    5. return child;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I call createMdiChild as:
    Qt Code:
    1. documentWindow *child = createMdiChild();
    2. child->show();
    To copy to clipboard, switch view to plain text mode 

    Thanks.
    Carlos.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: midi child does not close when I call close()

    I find it annoying that closing the widget contained by a QMdiSubWindow does not close the sub window itself, but that's the way it works.

    Try connecting to the close() slot of the QMDiSubWindow returned by QMdiArea::addSubWindow(). You will need to give documentWindow a closeMe() signal that is exposed so that you can make the connection in MainWindow::createMdiChild(). Then connect your push button signal to the new closeMe() signal to have it repeated outside documentWindow (yes I mean connect a signal to a signal).

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class docWindow: public QMainWindow {
    5. Q_OBJECT
    6.  
    7. public:
    8. docWindow(QWidget *p = 0): QMainWindow(p) {
    9. QPushButton *b = new QPushButton("Close", this);
    10. connect(b, SIGNAL(clicked()), this, SIGNAL(closeMe()));
    11. setCentralWidget(b);
    12. }
    13. ~docWindow() { }
    14.  
    15. signals:
    16. void closeMe();
    17.  
    18. };
    19.  
    20.  
    21. class MainWindow: public QMainWindow {
    22. Q_OBJECT
    23. public:
    24. MainWindow(QWidget *p = 0): QMainWindow(p) {
    25. m_mdiArea = new QMdiArea(this);
    26. setCentralWidget(m_mdiArea);
    27.  
    28. docWindow *dw = new docWindow(this);
    29. QMdiSubWindow *sw = m_mdiArea->addSubWindow(dw);
    30. connect(dw, SIGNAL(closeMe()), sw, SLOT(close()));
    31. }
    32. private:
    33. QMdiArea *m_mdiArea;
    34. };
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QApplication app(argc, argv);
    39.  
    40. MainWindow m;
    41. m.show();
    42.  
    43. return app.exec();
    44. }
    45. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 29th July 2011 at 04:37.

  7. The following user says thank you to ChrisW67 for this useful post:

    qlands (29th July 2011)

  8. #7
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: midi child does not close when I call close()

    Thanks... What I did instead of calling this.close() I call this->parentWidget.close() because as you said close will close the widget but not the parent window.

  9. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: midi child does not close when I call close()

    As long as you never use docWindow without a parent, or check for that circumstance, that will be fine. You could do something like:
    Qt Code:
    1. public slots:
    2. void closeMe() {
    3. QMdiSubWindows *sw = qobject_cast<QMdiSubWindow*>(parentWidget());
    4. if (sw)
    5. sw->close();
    6. else
    7. close();
    8. }
    To copy to clipboard, switch view to plain text mode 

    The app I borrowed from used the window either as an MDI child, a top-level window, or buried in a more complex UI and connected closeMe() accordingly.

Similar Threads

  1. QT application not close after close the mainwindow
    By artome in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 22:23
  2. Replies: 2
    Last Post: 6th May 2011, 08:02
  3. Replies: 2
    Last Post: 17th December 2010, 19:01
  4. Close child window notification in parent
    By doczorg in forum Newbie
    Replies: 1
    Last Post: 28th January 2010, 18:30
  5. Why is QBuffer's close call so slow?
    By eekisa in forum Qt Programming
    Replies: 4
    Last Post: 8th April 2009, 12:07

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.