PDA

View Full Version : Reopen the MainWindow



thibs
20th May 2015, 21:31
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:

void MainWindow::on_NewTest_clicked()
{
this->hide();
NewTest newtest;
newtest.setModal(true);
newtest.exec();

}

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!

wysota
20th May 2015, 22:22
To show a window simply call show() on it.

thibs
21st May 2015, 14:44
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:

void NewTest::on_Home_clicked()
{
MainWindow w;
w.show();

}

wysota
21st May 2015, 14:45
The object goes out of scope when the function ends.

thibs
21st May 2015, 15:21
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

stampede
21st May 2015, 16:21
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 ?

wysota
21st May 2015, 16:30
reopen the QMainWindow
Your code doesn't re-open anything. It creates a new window and shows it. And then it gets deleted.

thibs
21st May 2015, 16:41
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

anda_skoa
21st May 2015, 16:56
Do you want to show the window again without closing the dialog?

Cheers,
_

thibs
21st May 2015, 19:11
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:

MainWindow *mainWindow = new MainWindow();
mainWindow->show();

Thanks for the help guys!

wysota
21st May 2015, 21:15
Usually you'd open the window, show a modal dialog and then just close it, keeping the main window open all the time.

anda_skoa
22nd May 2015, 14:37
I want to reopen the mainwindow closing the dialog.
Then why don't you simply call show() after exec() returns?

Cheers,
_

d_stranz
23rd May 2015, 16:42
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:



void MainWindow::showDialog()
{
MyDialog dialog;

connect( &dialog, MyDialog::accepted, this, MainWindow::onDialogAccepted );
connect( &dialog, MyDialog::rejected, this, MainWIndow::onDialogRejected );

hide();
dialog.exec();
}

// A slot
void MainWindow::onDialogAccepted()
{
// You get here if the user clicks the OK, Yes, or whatever button causes the dialog to issue the accepted() signal
// So do something with it. If you need to get information from the dialog, do this:

MyDialog * pDialog = qobject_cast< MyDialog * >( sender() );
MyDialogInformation info = pDialog->getInformation();
saveInformationInApp( info );

// and finally, show the main window again:
show();
}

// And you do something similar to implement the slot MainWindow::onDialogRejected()


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:



void MainWindow::showDialog()
{
MyDialog dialog;

connect( &dialog, MyDialog::finished, this, MainWindow::onDialogFinished );

hide();
dialog.exec();
}

// A slot
void MainWindow::onDialogFinished( int result )
{
// You get here if the user clicks any button on the dialog to issue the finished() signal
// So do something with it. If you need to get information from the dialog, do this:

if ( QSialog::Accepted == result )
{
// The user clicked the OK button
MyDialog * pDialog = qobject_cast< MyDialog * >( sender() );
MyDialogInformation info = pDialog->getInformation();
saveInformationInApp( info );
}
else
{
// The user clicked some other button
}

// and finally, show the main window again:
show();
}


Or you could even do this more simply without slots:



void MainWindow::showDialog()
{
MyDialog dialog;

hide();

if ( QDialog::Accepted == dialog.exec() )
{
// Do something with the information entered in the dialog
}

show();
}


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

thibs
13th February 2017, 15:28
Thanks for the replies guys. Now i know how dumb my question was :p
Thanks for the patience

d_stranz
14th February 2017, 00:58
We were all dumb once. Now we are all smarter.