I stripped my problem down to what it necessary, so don't mind that is does nothing useful as shown here.
I have my MainWindow
which is a QMainWindow
with a QPushButton
inside, and I connected the clicked()
signal of that button to the following slot of the QMainWindow
:
void MainWindow::followingSlot()
{
OpenFileDialog(this);
}
OpenFileDialog
is a free function:
void OpenFileDialog(QWidget * inParent)
{
QFileDialog dialog(inParent, "caption");
dialog.setFileMode(QFileDialog::ExistingFiles);
dialog.setWindowModality(Qt::WindowModal);
dialog.exec();
}
It all works fine, until I have 2 or more MainWindow
s in play. Then, at the end of the QFileDialog::exec()
call, the current MainWindow
is pushed back one place in the 'focus stack' and the MainWindow
that was second becomes activated and jumps to the foreground. I'd like the window that was active and in front to stay active and in front.
I can of course adapt OpenFileDialog
to the following:
void OpenFileDialog(QWidget * inParent)
{
QFileDialog dialog(inParent, "caption");
dialog.setFileMode(QFileDialog::ExistingFiles);
dialog.setWindowModality(Qt::WindowModal);
dialog.exec();
inParent->raise();
inParent->activateWindow();
}
That brings my MainWindow
back to the front, but you can see the windows switching places briefly.
I found no similar problems via Google, QtCentre or SO. Is there a way to stop this behavior and keep the focus on the MainWindow
that had focus?
I'm working with Qt 4.8 on Mac.
EDIT:
I found that it's not the current MainWindow
that gets pushed back, it's the MainWindow
that is second in the stack, that gets pulled to the front. If I start with the left stack, I'll end up with the right stack (top being in front, bottom being at the back):
current MainWindow other MainWindow raise() current MainWindow
other application -> current MainWindow ------> other MainWindow
other MainWindow other application other application