Results 1 to 6 of 6

Thread: Why does my Qt application close when I still have a form open (but hidden)

  1. #1
    Join Date
    Oct 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Question Why does my Qt application close when I still have a form open (but hidden)

    This is what I wanted to do: a MainWindow launcher screen which, from a pushButton click event, goes hidden, shows MainWindow2, then when MainWindow2 is closed, is visible again.

    This is what I expected to do:

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. MainWindow2 *w = new MainWindow2(this);
    4. connect(w, SIGNAL(destroyed()), this, SLOT(show()));
    5. this->hide();
    6. w->show();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Alas, closing MainWindow2 actually closes the entire application. I ended up having to override MainWindow2's closingEvent to do this:

    Qt Code:
    1. void MainWindow2::closeEvent(QCloseEvent *event)
    2. {
    3. if (this->parentWidget() != 0)
    4. {
    5. this->parentWidget()->show();
    6. }
    7.  
    8. event->accept();
    9. }
    To copy to clipboard, switch view to plain text mode 

    This feels like a hack to me because logically I feel the application should never close as long as I never close the parent. Am I doing this wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why does my Qt application close when I still have a form open (but hidden)

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why does my Qt application close when I still have a form open (but hidden)

    Quote Originally Posted by wysota View Post
    How does that help? All it does is prevent the application from closing. I still can't get the child window to get the parent window to show before closing*, so I'm just left with a running process with no visible windows. Could you give an explicit example? And ideally explain why the signal connection in my first code example doesn't work?

    *without breaking encapsulation and modifying the child window's code by handling closeEvent.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why does my Qt application close when I still have a form open (but hidden)

    Quote Originally Posted by MachinTrucChose View Post
    How does that help? All it does is prevent the application from closing.
    Oh! You wanted me to write your program for you! Why didn't you say so in the first place? In that case.... no!

    Use signals and slots.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why does my Qt application close when I still have a form open (but hidden)

    Quote Originally Posted by wysota View Post
    Oh! You wanted me to write your program for you! Why didn't you say so in the first place? In that case.... no!

    Use signals and slots.
    I never asked for you to write anything. I just explained how that property doesn't fix the problem, as I still need to break encapsulation by having a child window call a parent window's code for things to work.

    Use signals and slots. You mean like in my first post's code sample which I said didn't work and drove me to post here? Or in my second post where I asked for an explanation as to why my initial signals/slots solution didn't work? Yeah, I can see how one could interpret that as PLEASE WRITE MY PROGRAM FOR ME!!!11.

    I'll wait for people who are genuinely helpful to respond.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why does my Qt application close when I still have a form open (but hidden)

    Quote Originally Posted by MachinTrucChose View Post
    I just explained how that property doesn't fix the problem,
    You can't do what you want without it.

    as I still need to break encapsulation by having a child window call a parent window's code for things to work.
    No, you don't. The fact that you came up with a solution that does that doesn't mean it is the only solution.

    Use signals and slots. You mean like in my first post's code sample which I said didn't work and drove me to post here?
    No, because your code is incorrect for two reasons. One is that a hidden widget is not destroyed, so destroyed() will not be emitted and second is that even if it was destroyed, the property you claim is useless would cause the application to close before the other window would have a chance to reveal itself.

    I'll wait for people who are genuinely helpful to respond.
    Don't tempt me to delete what I have already written just out of pure curiosity to see how long you would be waiting after what you have written. You have been given a good answer and you responded "and how does that help?". It helps to solve the problem you stated you had:

    Alas, closing MainWindow2 actually closes the entire application
    You can also read the question you gave in the thread title and then ask yourself again "how does that help".

    And now you can wait for people who are "genuinely helpful to respond".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 26th May 2011, 13:29
  2. Replies: 2
    Last Post: 17th December 2010, 19:01
  3. window style and form close query
    By eva2002 in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2010, 22:49
  4. Replies: 4
    Last Post: 23rd June 2008, 17:11
  5. Message on close(x) button of Form
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 09:39

Tags for this Thread

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.