简体   繁体   English

PySide6 中的错误 MDIWindow

[英]Buggy MDIWindow in PySide6

I'm using PySide6 and am having some weird glitch in my MDI Area.我正在使用 PySide6 并且在我的 MDI 区域中有一些奇怪的故障。

The image below shows only two spawned MDIsubwinows.下图仅显示了两个生成的 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.对于上下文,我使用 Qt Designer 生成 .ui 文件,然后将它们转换为 .py 文件。

单个 MDIAarea 页面的图像,2 个子窗口出现故障。

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()

You should not call show on a QMdiSubWindow before adding it to the area (which automatically shows it anyway).你不应该叫show将它添加到区域之前在QMdiSubWindow(自动显示也无妨)。

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. QMdiSubWindow 设置了一些特殊标志,如果您在将其添加到区域之前尝试调用show() ,理论上它将显示为正常的独立窗口。 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. show()还对小部件应用了一些更改,因为它正在初始化(并且在这种情况下作为顶级窗口),因此addSubWindow所做的事情addSubWindow变得有些不一致。

The solution is simple: remove all show() calls for sub windows.解决方案很简单:删除子窗口的所有show()调用。

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.请注意,在您的代码中,您没有为中央小部件设置正确的布局,而只是为horizontalLayoutWidget布局小部件设置了适当的布局,然后它会在主窗口内“浮动”而无法相应地调整其大小。 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.将 MdiArea 移到该小部件之外,将其删除,因为它没有用,右键单击主窗口的空白区域,然后从“布局”上下文菜单中选择合适的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM