PDA

View Full Version : How to center subwindows in qmdiarea?



David5292
27th April 2019, 03:39
Is there an attribute to position subwindows in qmdiarea? I’m trying to center subwindow in middle of mainwindow on startup (mdiarea)

I’m working on a useful example but haven’t finished it, wanted to see if anyone has tried doing this, and how they did it

Subwindows are randomly placed on startup when initialized




class App(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent=parent)
self.setupUi(self)
self.screenShape = QDesktopWidget().screenGeometry()
self.width = self.screenShape.width()
self.height = self.screenShape.height()
self.resize(self.width * .6, self.height * .6)
self.new = []
#calls GUI's in other modules
self.lw = Login()
self.vs = VS()
self.ms = MS()
self.hw = HomeWindow()
self.mw = MainWindow()
self.ga = GA()
self.sGUI = Settings()
# shows subwindow
self.CreateLogin()
self.CreateVS()
self.CreateMS()
self.CreateGA()
self.CreateSettings()

def CreateLogin(self):
self.subwindow = QMdiSubWindow()
self.subwindow.setWidget(self.lw)
self.subwindow.setAttribute(Qt.WA_DeleteOnClose, True)
self.mdiArea.addSubWindow(self.subwindow)
self.subwindow.setMaximumSize(520, 300)
self.subwindow.setMinimumSize(520, 300)
self.lw.showNormal()

def CreateVS(self):
self.subwindow = QMdiSubWindow()
self.subwindow.setWidget(self.vs)
self.mdiArea.addSubWindow(self.subwindow)
self.vs.showMinimized()

def CreateMS(self):
self.subwindow = QMdiSubWindow()
self.subwindow.setWidget(self.ms)
self.mdiArea.addSubWindow(self.subwindow)
self.ms.showMinimized()
self.ms.tabWidget.setCurrentIndex(0)

def CreateGA(self):
self.subwindow = QMdiSubWindow()
self.subwindow.setWidget(self.ga)
self.mdiArea.addSubWindow(self.subwindow)
self.ga.showMinimized()
self.subwindow.setMaximumSize(820, 650)

def CreateSettings(self):
self.subwindow = QMdiSubWindow()
self.subwindow.setWidget(self.sGUI)
self.mdiArea.addSubWindow(self.subwindow)
self.sGUI.showMinimized()

def CreateWindow(self):
self.hw.pushButton.clicked.connect(self.vs.showNor mal)
self.hw.pushButton_2.clicked.connect(self.Modulepr ogram)
self.hw.pushButton_3.clicked.connect(self.ms.showN ormal)
self.hw.pushButton_4.clicked.connect(self.ga.showN ormal)
self.subwindow = QMdiSubWindow()
self.subwindow.setWindowFlags(Qt.CustomizeWindowHi nt | Qt.Tool)
self.subwindow.setWidget(self.hw)
self.subwindow.setMaximumSize(258, 264)
self.subwindow.move(self.newwidth*.35, self.newheight*.25)
self.mdiArea.addSubWindow(self.subwindow)

d_stranz
28th April 2019, 03:22
No widget geometry will be correct until after the widget's showEvent(), so you can't rely on any geometry calculations made in the constructor (__init__). You also cannot rely on any calculations in the resizeEvent() prior to the window's showEvent().

So after the main window has been shown (isVisible() returns true), you can then get the geometry. The geometry you want is that of the QMdiArea. Find its center by retrieving its rect() and then the center() of that. To center your QMdiSubWindow, get its frameGeometry() (or use whatever size you intend to create it with). Divide the height() and width() of that in half, subtract the half-width from the x coordinate of the MDI area's center and the half-height from the y coordinate of the MDI center, and use the result as the argument to setPos() on the MDI subwindow. This will give you an MDI area and an MDI subwindow that have the same center coordinate, that is, the subwindow will be centered in the MDI area. Once that's done, you can show() the subwindow.