Search code examples
pythonuser-interfacepyqt5locationwindow

How to place new window on existing main window location in PyQt5 (not center of screen)?


I have a project where the user presses a button that opens a new window. How can i set my new windows to always open on top of the main windows location? (when the user moves the windows dont open it on the center of screen, insted on top of the main window location)

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

class MainWindow(QtWidgets.QMainWindow):

def __init__(self):
    super(MainWindow, self).__init__()
    self.mainUI()

def mainUI(self):
    self.setWindowTitle('Estetica Arch Bt.')
    self.setGeometry(0,0,400,400)

    self.button = QPushButton('Open',self)
    self.button.clicked.connect(self.on_click_button)

    self._main = QtWidgets.QWidget()
    self.setCentralWidget(self._main)
    layout = QtWidgets.QGridLayout(self._main)
    layout.addWidget(self.button)

    self.show()

@pyqtSlot()
def on_click_button(self):
    self.pdw = ProjektDetailsWindow()
    self.pdw.show()


class ProjektDetailsWindow(QWidget):

def __init__(self):
    super().__init__()
    self.projektdetails()
    

def projektdetails(self):
    self.setFixedSize(200, 200)
    self.show()


def main():
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Solution

  • You can move() new window where you want right after show()ing it.

    @pyqtSlot()
    def on_click_button(self):
        self.pdw = ProjektDetailsWindow()
        self.pdw.show()
        self.pdw.move(self.pos())