Search code examples
pythonpyqtpyqt5qmainwindowqdialog

how to make a dialog or a popup window can stay behind the mainwindow and don't cover the mainwindow


I am new to pyqt5, I want pop-up a window and let the window can stay behinde the mainwindow and not clickable(smiliar like let the new window as an another process window)

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Demo(QMainWindow):
    def __init__(self):
        super().__init__()
        test_button = QPushButton('test')
        test_button.clicked.connect(self.onClick)
        self.setCentralWidget(test_button)

    def onClick(self):
        # dlg = QDialog(self)
        dlg = QMainWindow(self)
        dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
        dlg.show()

app = QApplication([])
demo = Demo()
demo.show()
app.exec()

the new window is always stay on the mainwindow, I need to the window stay behide it.


Solution

  • void QWidget::move(int x, int y)

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    
    
    class Demo(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle('Main Window')
            test_button = QPushButton('test')
            test_button.clicked.connect(self.onClick)
            self.setCentralWidget(test_button)
    
        def onClick(self):
            # dlg = QDialog(self)
            dlg = QMainWindow(self)
            dlg.setWindowTitle('Dialog Window')
    
            dlg.move(self.geometry().x() + self.geometry().width() + 30,         # <---
                     self.geometry().y() - 30)
    
            dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
            dlg.show()
    
        def sizeHint123(self):
            return QSize(200, 200)
    
    app = QApplication([])
    demo = Demo()
    demo.show()
    app.exec()
    

    enter image description here


    Update

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    
    
    class Demo(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle('Main Window')
    
            self.setWindowFlags(Qt.WindowStaysOnTopHint)                            # +++
    
            test_button = QPushButton('test')
            test_button.clicked.connect(self.onClick)
            self.setCentralWidget(test_button)
    
        def onClick(self):
            # dlg = QDialog(self)
            self.dlg = QMainWindow()                                                 # --- self
            self.dlg.setWindowTitle('Dialog Window')
    
            self.dlg.move(self.geometry().x() + self.geometry().width() + 30,         
                     self.geometry().y() - 30)
    
    #        dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
            self.dlg.show()
    
        def sizeHint123(self):
            return QSize(200, 200)
    
    app = QApplication([])
    demo = Demo()
    demo.show()
    app.exec()
    

    enter image description here