'QDialog: How to enable being behind the parent (main window) on Windows?
I use a QDialog. In the constructor, I set setWindowFlags(Qt::Window). On Linux/KDE, the effect is that the dialog gets it's own window bar button and can go behind it's parent (the main window). That's what I want to have.
On Windows however, the dialog does not have an own window bar button, and it's always on top of the main window. Is it possible to achieve the same behavior on Windows as I have on Linux?
I played around with the Qt::WindowStaysOnTopHint window flag, but it had no effect.
Solution 1:[1]
Pass your QDialog a NULL parent, then it is independent of your main window and can go behind the main window.
Then in your main window's closeEvent() function, call myDialog->close(). This will shut down the dialog when your main window exits.
Solution 2:[2]
setup your dialog parent by sending its this pointer to your dialog constructor. for example in if your are showing a QFileDialog for staying top use
QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
instead of :
QFileDialog::getOpenFileName(NULL,
tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));
Solution 3:[3]
I had this same issue. I found that if you use the StaysOnTopHint, the dialogs would stay on top of even other windows (not just the main application window). This did the trick for me. The code is in Python but should be easily translatable to C++. Here self.parent is the application main window.
if staysOnTop:
dialog.setParent(self.parent, Qt.Dialog | Qt.WindowMinimizeButtonHint | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint)
else:
dialog.setParent(None, dialog.windowFlags() & ~Qt.WindowStaysOnTopHint)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JimmyG |
| Solution 2 | Nima Mohammadi |
| Solution 3 | ak22 |
