Results 1 to 15 of 15

Thread: Reopen the MainWindow

  1. #1
    Join Date
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Reopen the MainWindow

    Hi guys! I was trying to show mainwindow after to hide that, but I couldn`t do that.

    First I hide the mainwindow in the pushbutton action and open a dialog window:
    Qt Code:
    1. void MainWindow::on_NewTest_clicked()
    2. {
    3. this->hide();
    4. NewTest newtest;
    5. newtest.setModal(true);
    6. newtest.exec();
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then in this dialog window I would like to add a button to return to the mainwindow, but I searched and I didn`t found anything about it. Thanks guys!

  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: Reopen the MainWindow

    To show a window simply call show() on it.
    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
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reopen the MainWindow

    I tried that but it didn`t work. Just show a window for half of a second and then close it. I tried to call MainWindow using this code:
    Qt Code:
    1. void NewTest::on_Home_clicked()
    2. {
    3. MainWindow w;
    4. w.show();
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

  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: Reopen the MainWindow

    The object goes out of scope when the function ends.
    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
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reopen the MainWindow

    So, how do I do that? I know how to open and close QDialog, but in the QDialog I would like to have a button that close the QDialog and reopen the QMainWindow

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Reopen the MainWindow

    So, how do I do that?
    The object goes out of scope when the function ends.
    Do you understand what is the meaning of the above sentence ?

  7. #7
    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: Reopen the MainWindow

    Quote Originally Posted by thibs View Post
    reopen the QMainWindow
    Your code doesn't re-open anything. It creates a new window and shows it. And then it gets deleted.
    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.


  8. #8
    Join Date
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reopen the MainWindow

    I understand, I just wanted to show what I was trying to do. Do you guys know how do I show the MainWindow inside a pushbutton event in another class after it be hidden

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reopen the MainWindow

    Do you want to show the window again without closing the dialog?

    Cheers,
    _

  10. #10
    Join Date
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Lightbulb Re: Reopen the MainWindow

    Quote Originally Posted by anda_skoa View Post
    Do you want to show the window again without closing the dialog?

    Cheers,
    _
    I want to reopen the mainwindow closing the dialog. My mainwindow has some buttons such as "new user" that open the dialog new user. Then I would like to have a button in this dialog that close the dialog and return to the mainWindow; For example, "If you are done of register new user press here to return to home". The only way that I found is leave the mainWindow open and just open/close the dialog.


    Added after 48 minutes:


    Actually I found a way to solve my problem. I use this line of code in the dialog.cpp after hide my mainwindow:
    Qt Code:
    1. MainWindow *mainWindow = new MainWindow();
    2. mainWindow->show();
    To copy to clipboard, switch view to plain text mode 

    Thanks for the help guys!
    Last edited by thibs; 21st May 2015 at 20:11.

  11. #11
    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: Reopen the MainWindow

    Usually you'd open the window, show a modal dialog and then just close it, keeping the main window open all the time.
    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.


  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reopen the MainWindow

    Quote Originally Posted by thibs View Post
    I want to reopen the mainwindow closing the dialog.
    Then why don't you simply call show() after exec() returns?

    Cheers,
    _

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reopen the MainWindow

    Actually I found a way to solve my problem. I use this line of code in the dialog.cpp after hide my mainwindow:

    MainWindow *mainWindow = new MainWindow();
    mainWindow->show();
    Do you understand that this creates a different MainWindow instance from the one you used to open the dialog in the first place? Presumably in your MainWindow code somewhere you are creating the dialog on the stack, hiding the original MainWindow instance, then calling exec on the dialog. If you add the above code into the dialog class, you now have two MainWindow instances, one visible, one hidden. The new one has none of the changed state (if there was any) that occurred before the dialog was opened. It's a newly born MainWindow, with whatever defaults a new MainWindow instance has. Whatever changes the user made are tucked away in the first MainWindow instance, the hidden one.

    And then after a while, the user clicks the button again. When that dialog closes, you'll now have three MainWindow instances, 2 hidden, 1 visible, all with possibly different state. Will the real MainWindow instance please stand up?

    What you want to do is use signals and slots to control the visibility of the first (and only) MainWindow. Use code something like this:

    Qt Code:
    1. void MainWindow::showDialog()
    2. {
    3. MyDialog dialog;
    4.  
    5. connect( &dialog, MyDialog::accepted, this, MainWindow::onDialogAccepted );
    6. connect( &dialog, MyDialog::rejected, this, MainWIndow::onDialogRejected );
    7.  
    8. hide();
    9. dialog.exec();
    10. }
    11.  
    12. // A slot
    13. void MainWindow::onDialogAccepted()
    14. {
    15. // You get here if the user clicks the OK, Yes, or whatever button causes the dialog to issue the accepted() signal
    16. // So do something with it. If you need to get information from the dialog, do this:
    17.  
    18. MyDialog * pDialog = qobject_cast< MyDialog * >( sender() );
    19. MyDialogInformation info = pDialog->getInformation();
    20. saveInformationInApp( info );
    21.  
    22. // and finally, show the main window again:
    23. show();
    24. }
    25.  
    26. // And you do something similar to implement the slot MainWindow::onDialogRejected()
    To copy to clipboard, switch view to plain text mode 

    You could implement something slightly different, and connect a slot to the QDialog::finished( int ) signal, in which case you would have to look at the value of the argument passed in the signal and do something with it:

    Qt Code:
    1. void MainWindow::showDialog()
    2. {
    3. MyDialog dialog;
    4.  
    5. connect( &dialog, MyDialog::finished, this, MainWindow::onDialogFinished );
    6.  
    7. hide();
    8. dialog.exec();
    9. }
    10.  
    11. // A slot
    12. void MainWindow::onDialogFinished( int result )
    13. {
    14. // You get here if the user clicks any button on the dialog to issue the finished() signal
    15. // So do something with it. If you need to get information from the dialog, do this:
    16.  
    17. if ( QSialog::Accepted == result )
    18. {
    19. // The user clicked the OK button
    20. MyDialog * pDialog = qobject_cast< MyDialog * >( sender() );
    21. MyDialogInformation info = pDialog->getInformation();
    22. saveInformationInApp( info );
    23. }
    24. else
    25. {
    26. // The user clicked some other button
    27. }
    28.  
    29. // and finally, show the main window again:
    30. show();
    31. }
    To copy to clipboard, switch view to plain text mode 

    Or you could even do this more simply without slots:

    Qt Code:
    1. void MainWindow::showDialog()
    2. {
    3. MyDialog dialog;
    4.  
    5. hide();
    6.  
    7. if ( QDialog::Accepted == dialog.exec() )
    8. {
    9. // Do something with the information entered in the dialog
    10. }
    11.  
    12. show();
    13. }
    To copy to clipboard, switch view to plain text mode 

    But you also understand, of course, that if you implement this behaviour, then your app will act like no other app on the planet except for those like it that users hate because they do unexpected things and confuse them.

    What if you were in the middle of working on an important document (like writing your resume to get that great programming job), and when you clicked a button that opens a dialog, suddenly the window with all of your work in it disappeared? Oh my god, where did my resume go? I've been working on that for the last hour and it's gone! You wouldn't ever use that app again, would you, even if your document came back when you closed the dialog (or didn't, the way you originally implemented this)? You could never trust that it wouldn't lose your work - you wouldn't know if clicking that button caused a crash or not because in both cases, the main window disappears.

    There is a reason why the behaviour that Wysota points out is the standard in nearly all apps (except the ones you only use once before uninstalling).

  14. #14
    Join Date
    May 2015
    Posts
    22
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reopen the MainWindow

    Thanks for the replies guys. Now i know how dumb my question was
    Thanks for the patience

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reopen the MainWindow

    We were all dumb once. Now we are all smarter.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. mainwindow
    By kaals in forum Newbie
    Replies: 1
    Last Post: 25th August 2012, 21:25
  2. Replies: 0
    Last Post: 6th November 2011, 10:22
  3. Replies: 1
    Last Post: 12th April 2011, 10:53
  4. Replies: 1
    Last Post: 6th July 2010, 13:03
  5. MainWindow
    By amagdy.ibrahim in forum Newbie
    Replies: 6
    Last Post: 14th June 2008, 15:12

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.