I wish to create a project including 3 windows: mainWindow, childWindow1, childWindow2, and only one window should show at a time. And I can switch between these windows.
So I have three tasks:
- I place two buttons in the mainWindow and want to use them to make one of the child windows showing and the main window hiding.
- And when I close the child window, I wish to show the main window.
- When I close the main window, terminate the whole application.
I first had a problem: If I close the child window, the application exit. So I use the qApp.setQuitOnLastWindowClosed(false), and I got task 2 done. But another problem occured: If I close the main window, the program is still running.
Last problem: How to show the child window in the task bar? It looks wired to run a program which can't be found in the taskbar.
I search everywhere I could, any help would be really appreciated!
main.cpp:
int main()
{
QApplication a(argc, argv);
a.setQuitOnLastWindowClosed(false);
MainWindow mainWindow;
mainWindow.show();
return a.exec();
}
mainWindow.cpp:
void mainWindow::button1Clicked()
{
this->hide();
childWindow1 = new ChildWindow1(this);
connect(childWindow1, &QMainWindow::destroyed, this, &QMainWindow::show);
childWindow1->setWindowModality(Qt::WindowModal);
childWindow1->show();
}
childWindow1.cpp
ChildWindow1::ChildWindow1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
}