Can't bring to foreground the MainWindow
-
Hello,
I stacked on a problem on my app.
I have a MainWindow with a button that simply opens a second MainWindow. What I'm trying to do is to close both the windows when the first MainWindow is destroyed and to bring to foreground the mainwindow that is selected.To do that I tried two different code.
The first is this:
void MainWindow::on_actionArchivio_Sessioni_triggered()
{
if(archivio == NULL){
archivio = new Archivio(this);
archivio->show();
}
else
{
archivio->activateWindow();
}
}archivio is declared as pointer in the mainwindow.h and it's my second mainwindow. Here I open it when i click a button on the toolbar and if the archivio window already exists I only set the archivio as activewindow so that I don't create it every time I click the button.
In this way the first window is Parent of the archivio window, and if I close the mainwindow also the archivio window will close. But I can't bring the MainWindow to foreground, I can select it, but the archivio window is always on the top. I haven't used any Qt::WindowStaysOnTopHint or somthing like that, if you are wondering.
The second code I tried is this:
void MainWindow::on_actionArchivio_Sessioni_triggered()
{
if(archivio == NULL){
archivio = new Archivio;
archivio->show();
}
else
{
archivio->activateWindow();
}
}Without telling the archivio window who is its parent, I can easily switch between the two windows, if I select the MainWindow it will be on top and it's the same for the archivio window that it's exactly what i'm looking for.
But if I close my MainWindow the archivio window is still there, of course.I tried to catch when the mainwindow closes:
MainWindow::~MainWindow()
{
delete ui;
}when the mainwindow is closed it should pass here, but it doesn't. until I don't close also the archivio window the program doesn't reach this point. What I wanted to do here was to call the archivio->close() so that when I close the mainwindow I can close also the archivio window.
I don't know which one of these two options is the best, anyway I need to solve one problem on both sides, so if anyone has any ideas it would be perfect.
I think that the first option it's the most correct way, but I can't bring the mainwindow on the top.
thank you all,
Davide -
Hi,
Why not reimplement the closeEvent method in your MainWindow and close the archivio there ?