Issue
I'm using PySide6 and am having some weird glitch in my MDI Area.
The image below shows only two spawned MDIsubwinows. When I go to drag the window, all historical positenter code hereions stay, even when I a move another menu after. Its also super laggy.
For context, I am using Qt Designer to generate .ui files then convert them to .py files.
Here is my code:
from PySide6.QtCore import QRect, QCoreApplication, QMetaObject
from PySide6.QtWidgets import QWidget, QHBoxLayout, QMainWindow, QMdiArea, QMenu, QMenuBar, QMdiSubWindow, QApplication
# Converted .ui file from Qt Designer
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(1920, 1080)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.horizontalLayoutWidget = QWidget(self.centralwidget)
self.horizontalLayoutWidget.setObjectName(u"horizontalLayoutWidget")
self.horizontalLayoutWidget.setGeometry(QRect(0, 0, 1920, 1054))
self.horizontalLayout = QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setSpacing(7)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.mdiArea = QMdiArea(self.horizontalLayoutWidget)
self.mdiArea.setObjectName(u"mdiArea")
self.horizontalLayout.addWidget(self.mdiArea)
MainWindow.setCentralWidget(self.centralwidget)
self.menuBar = QMenuBar(MainWindow)
self.menuBar.setObjectName(u"menuBar")
self.menuBar.setGeometry(QRect(0, 0, 1920, 26))
self.menuFile = QMenu(self.menuBar)
self.menuFile.setObjectName(u"menuFile")
self.menuEdit = QMenu(self.menuBar)
self.menuEdit.setObjectName(u"menuEdit")
self.menuView = QMenu(self.menuBar)
self.menuView.setObjectName(u"menuView")
self.menuPreferences = QMenu(self.menuBar)
self.menuPreferences.setObjectName(u"menuPreferences")
self.menuWindow = QMenu(self.menuBar)
self.menuWindow.setObjectName(u"menuWindow")
self.menuHelp = QMenu(self.menuBar)
self.menuHelp.setObjectName(u"menuHelp")
self.menuTools = QMenu(self.menuBar)
self.menuTools.setObjectName(u"menuTools")
MainWindow.setMenuBar(self.menuBar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
# retranslateUi
# Converted .ui file from Qt Designer
# Custom class to house the application.
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.initialize_subwindows()
self.ui.mdiArea.tileSubWindows()
# build the menus in readable chunks
# ///////////////////////////////////////////////////////////////
def initialize_subwindows(self):
# Enables the windows to last longer than their create function calls.
self.subwindow_dict = {}
# Create all the subwindows. they should initialize into the subwindows_dict
self.create_build_overview_subwindow()
self.create_object_properties_subwindow()
for window in self.subwindow_dict.values():
self.ui.mdiArea.addSubWindow(window)
self.subwindow_dict.clear()
# the next two functions create subwindows and add them to the main collection of subwindows for the MDI area
def create_build_overview_subwindow(self):
build_overview_window = QMdiSubWindow()
build_overview_window.setWindowTitle('Build Overview')
build_overview_window.show()
self.subwindow_dict.update({'build_overview':build_overview_window})
def create_object_properties_subwindow(self):
object_properties_window = QMdiSubWindow()
object_properties_window.setWindowTitle('Object Properties')
object_properties_window.show()
# Return a dict to add to the subwindow list
# for object perminance
self.subwindow_dict.update({'object_properties':object_properties_window})
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
Solution
You should not call show
on a QMdiSubWindow before adding it to the area (which automatically shows it anyway).
A QMdiSubWindow has some special flags set, if you try to call show()
before adding it to the area it will theoretically be shown as a normal, independent window. show()
also applies some changes to the widget since it's being initialized (and as a top level window, in this case), so what addSubWindow
does becomes somehow inconsistent for that reason.
The solution is simple: remove all show()
calls for sub windows.
Note that in your code you didn't set a proper layout for the central widget, but only for the horizontalLayoutWidget
which then becomes "floating" inside the main window without being able to adapt its size accordingly. Move the MdiArea outside that widget, remove it since it's useless, right click on an empty space of the main window and select a proper layout from the "Lay out" context menu.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.