PDA

View Full Version : Double window issue



georgeky
31st January 2011, 05:26
I have a small app that launches a new window from a pushbutton click. is there anyway to detect the 2nd window and not open a new one until the old one is closed?

psedo code.

void MainWindow::on_Button_clicked()
{
MainForm* NewWindow;
NewWindow = new MainForm();
NewWindow->show();

}

Any help is appreciated.

mohjlir
31st January 2011, 06:13
The easiest way I can think of to do this is to pass a pointer to the parent window to NewWindow, and use the pointer to call a function in MainWindow in the destructor of NewWindow.

Something like:



NewWindow::NewWindow(QWidget* parent)
: QWidget(parent)
{
// Whatever
}
~NewWindow::NewWindow()
{
(MainWindow*)parentWidget()->WindowClosed();
}


You could then implement WindowClosed() something like this:



void MainWindow::WindowClosed()
{
childWindowClosed = true;
}


If there's a better way to do this, someone else might chime in.

tbscope
31st January 2011, 06:14
There are several techniques.
But I think the most correct one would be to store the state of a window into a variable.
Suppose you have a main window, create a member called something like "bool window2Visible". Set it to true when it is open and to false if it isn't. Use signals and slots to check the state.

You could also test for the validity of a pointer.You might need to set some widget flags here.
Or, if you only want one instance of one window open at any time, look into singletons.
...