如何在取消停靠后调整几何图形后触发 QTreeView 的更新

How to Trigger an update of the QTreeView after adjusting the Geometry after undocking

使用: Python 3.7 pyqt5 Windows10

取消停靠时,我将主 window 的大小调整为没有 DockWindow 时的大小,但这样做时右侧的两列消失了。我尝试了各种事件来尝试触发对该 TreeView 的调整,但无济于事。我包含的代码有问题,只需单击其中一个 Docks,然后将结果 window 从 Main Window 中分离出来,您就会明白我的意思。一如既往,我们将不胜感激。

我试过以下方法:

self.updateGeometry()
self.CenterPane.updateGeometry()
self.CenterPane.ItemDsply.updateGeometry()

self.resizeEvent(QResizeEvent(self.size(), QSize()))
self.CenterPane.resizeEvent(QResizeEvent(self.size(), QSize()))
self.CenterPane.ItemDsply.resizeEvent(QResizeEvent(self.size(), QSize()))

这是一个工作示例:

from sys  import exit as sysExit
from math import trunc

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

class CustomItemModel(QStandardItemModel):
    def headerData(self, section, orientation, role):
        if role == Qt.ForegroundRole:
            brush = QBrush()
            brush.setColor(Qt.blue)
            brush.setStyle(Qt.SolidPattern)
            return brush

        elif role == Qt.BackgroundRole:
            brush = QBrush()
            brush.setColor(Qt.yellow)
            brush.setStyle(Qt.SolidPattern)
            return brush

        elif role == Qt.FontRole:
            font = QFont()
            font.setBold(True)
            font.setPointSize(10)
            return font

        return super().headerData(section, orientation, role)

class DockWin1(QDockWidget):
    def __init__(self, parent):
        QDockWidget.__init__(self)

        self.setWindowTitle('Dock 1')
        self.MainWin = parent

        self.container = QWidget(self)
        self.container.setLayout(QHBoxLayout())
        self.setWidget(self.container)
        self.container.layout().addWidget(QTextEdit())
        self.setMinimumWidth(100)
        self.setMinimumHeight(100)

        self.topLevelChanged.connect(self.SetDock1Free)

    @property
    def MainWin(self):
        return self.__parent

    @MainWin.setter
    def MainWin(self, value):
        self.__parent = value

    def closeEvent(self, event):
        self.MainWin.ClosingDock1()

    def SetDock1Free(self):
        if self.MainWin.Dock1Free:
            self.MainWin.CheckToResize('Dock1', 100)
        else:
            self.MainWin.CheckToResize('Dock1', self.width())

        self.MainWin.Dock1Free = not self.MainWin.Dock1Free

class DockWin2(QDockWidget):
    def __init__(self, parent):
        QDockWidget.__init__(self)

        self.setWindowTitle('Dock 2')
        self.MainWin = parent

        self.container = QWidget(self)
        self.container.setLayout(QHBoxLayout())
        self.setWidget(self.container)
        self.container.layout().addWidget(QTextEdit())
        self.setMinimumWidth(100)
        self.setMinimumHeight(100)

        self.topLevelChanged.connect(self.SetDock2Free)

    @property
    def MainWin(self):
        return self.__parent

    @MainWin.setter
    def MainWin(self, value):
        self.__parent = value

    def closeEvent(self, event):
        self.MainWin.ClosingDock2()

    def SetDock2Free(self):
        if self.MainWin.Dock2Free:
            self.MainWin.CheckToResize('Dock2', 100)
        else:
            self.MainWin.CheckToResize('Dock2', self.width())

        self.MainWin.Dock2Free = not self.MainWin.Dock2Free

class ItemDsplyr(QTreeView):
    def __init__(self, CentrPane):
        QTreeView.__init__(self, CentrPane)
        self.CntrPane = CentrPane

        self.setEditTriggers(QTreeView().NoEditTriggers)
        self.model = CustomItemModel(0, 3)
        self.model.setHorizontalHeaderLabels(['1st Col', '2nd Col', '3rd Col'])
        self.model.setHeaderData(1, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole)
        self.setModel(self.model)
        self.setMinimumWidth(250)

        self.header().setStretchLastSection(False)
        self.header().setSectionResizeMode(0, QHeaderView.Stretch)
        self.setColumnWidth(1, 75)
        self.setColumnWidth(2, 100)

class CenterPanel(QWidget):
    def __init__(self, MainWin):
        QWidget.__init__(self)

        self.MyEditor = QTextEdit('Editorial')
        self.ItemDsply = ItemDsplyr(self)

        CntrPane = QSplitter(Qt.Horizontal, self)
        CntrPane.addWidget(self.MyEditor)
        CntrPane.addWidget(self.ItemDsply)
        CntrPane.setSizes([50,200])
        CntrPane.setCollapsible(0, False)
        CntrPane.setCollapsible(1, False)

        hbox = QHBoxLayout(self)
        hbox.addWidget(CntrPane)

        self.setLayout(hbox)

class MenuToolBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
        self.MainWin = MainWin
        self.MainMenu = MainWin.menuBar()

        self.WndowMenu  = self.MainMenu.addMenu('Windows')

        self.Dock1Act = QAction('Dock1', self)
        self.Dock1Act.setStatusTip('Open the Dock1 Window')
        self.Dock1Act.triggered.connect(MainWin.ShowDock1)

        self.Dock2Act = QAction('Dock2', self)
        self.Dock2Act.setStatusTip('Open the Dock2 Window')
        self.Dock2Act.triggered.connect(MainWin.ShowDock2)

        self.WndowMenu.addAction(self.Dock1Act)
        self.WndowMenu.addSeparator()
        self.WndowMenu.addAction(self.Dock2Act)

        self.InitToolBar(MainWin)

    def InitToolBar(self, MainWin):
        self.mainToolBar = MainWin.addToolBar("Quick Access")

        self.mainToolBar.addAction(self.Dock1Act)
        self.mainToolBar.addSeparator()
        self.mainToolBar.addAction(self.Dock2Act)

class UI_MainWindow(QMainWindow):
    def __init__(self, MainDesktop):
        super(UI_MainWindow, self).__init__(MainDesktop)
        self.setWindowTitle('Main Window')
        self.Dock1Open = False
        self.Dock1Free = False
        self.Dock2Open = False
        self.Dock2Free = False

        self.MnDskTop = MainDesktop

      # Left, Top, Width, Height
        self.setGeometry(200, 200, 550, 550)

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.MenuToolBar = MenuToolBar(self)

    def ShowDock1(self):
        if not self.Dock1Open:
            self.CheckToResize('Dock1', 300)
            self.Dock1 = DockWin1(self)
            self.Dock1Open = True
            self.Dock1Free = False
            self.addDockWidget(Qt.RightDockWidgetArea, self.Dock1)

    def ClosingDock1(self):
        self.Dock1Open = False

    def ShowDock2(self):
        if not self.Dock2Open:
            self.CheckToResize('Dock2', 300)
            self.Dock2 = DockWin2(self)
            self.Dock2Open = True
            self.Dock2Free = False
            self.addDockWidget(Qt.RightDockWidgetArea, self.Dock2)

    def ClosingDock2(self):
        self.Dock2Open = False

    def CheckToResize(self, WinDocked, DockWdth):
      # If any of the other docks are open and docked do not resize
        if   self.Dock1Open and WinDocked != 'Dock1' and not self.Dock1Free:
            return
        elif self.Dock2Open and WinDocked != 'Dock2' and not self.Dock2Free:
            return

        Docking = True
        if   WinDocked == 'Dock1' and self.Dock1Open != self.Dock1Free:
            Docking = False
        elif WinDocked == 'Dock2'  and self.Dock2Open != self.Dock2Free:
            Docking = False

        WinLeft  = self.geometry().left()
        WinTop   = self.geometry().top()
        WinWidth = self.geometry().width()
        WinHight = self.geometry().height()
        ScrWidth = self.MnDskTop.screenGeometry().width()

      # Docking
        if Docking:
            if ScrWidth < (WinLeft + WinWidth + DockWdth):
                WinLeft  = 0
                WinWidth = ScrWidth
            elif WinLeft > (trunc(DockWdth/2)):
                WinLeft = WinLeft - trunc(DockWdth/2)
                WinWidth = WinWidth + DockWdth
            else:
                WinLeft = 0
                WinWidth = WinWidth + DockWdth

            self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
     # Un-Docking
        else:
            WinWidth = WinWidth - DockWdth
            self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
          # Call the Event that Triggers View Adjustment ?? 
            self.CenterPane.ItemDsply.updateGeometry()
#            self.CenterPane.ItemDsply.resizeEvent(QResizeEvent(self.size(), QSize()))

if __name__ == '__main__':
    MainApp = QApplication([])

    MainGui = UI_MainWindow(MainApp.desktop())
    MainGui.show()

    sysExit(MainApp.exec_())

如前所述,我正在尝试让 QTreeView 适当调整以适应新大小 window,以便它显示所有三列。目前它将最后一列推到视图之外。

请求的图片:(查看评论)

void QHeaderView::setSectionResizeMode(int logicalIndex, QHeaderView::ResizeMode mode)

Sets the constraints on how the section specified by logicalIndex in the header can be resized to those described by the given mode. The logical index should exist at the time this function is called.

from sys  import exit as sysExit
from math import trunc

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

class CustomItemModel(QStandardItemModel):
    def headerData(self, section, orientation, role):
        if role == Qt.ForegroundRole:
            brush = QBrush()
            brush.setColor(Qt.blue)
            brush.setStyle(Qt.SolidPattern)
            return brush

        elif role == Qt.BackgroundRole:
            brush = QBrush()
            brush.setColor(Qt.yellow)
            brush.setStyle(Qt.SolidPattern)
            return brush

        elif role == Qt.FontRole:
            font = QFont()
            font.setBold(True)
            font.setPointSize(10)
            return font

        return super().headerData(section, orientation, role)

class DockWin1(QDockWidget):
    def __init__(self, parent):
        QDockWidget.__init__(self)

        self.setWindowTitle('Dock 1')
        self.MainWin = parent

        self.container = QWidget(self)
        self.container.setLayout(QHBoxLayout())
        self.setWidget(self.container)
        self.container.layout().addWidget(QTextEdit())
        self.setMinimumWidth(100)
        self.setMinimumHeight(100)

        self.topLevelChanged.connect(self.SetDock1Free)

    @property
    def MainWin(self):
        return self.__parent

    @MainWin.setter
    def MainWin(self, value):
        self.__parent = value

    def closeEvent(self, event):
        self.MainWin.ClosingDock1()

    def SetDock1Free(self):
        if self.MainWin.Dock1Free:
            self.MainWin.CheckToResize('Dock1', 100)
        else:
            self.MainWin.CheckToResize('Dock1', self.width())

        self.MainWin.Dock1Free = not self.MainWin.Dock1Free

class DockWin2(QDockWidget):
    def __init__(self, parent):
        QDockWidget.__init__(self)

        self.setWindowTitle('Dock 2')
        self.MainWin = parent

        self.container = QWidget(self)
        self.container.setLayout(QHBoxLayout())
        self.setWidget(self.container)
        self.container.layout().addWidget(QTextEdit())
        self.setMinimumWidth(100)
        self.setMinimumHeight(100)

        self.topLevelChanged.connect(self.SetDock2Free)

    @property
    def MainWin(self):
        return self.__parent

    @MainWin.setter
    def MainWin(self, value):
        self.__parent = value

    def closeEvent(self, event):
        self.MainWin.ClosingDock2()

    def SetDock2Free(self):
        if self.MainWin.Dock2Free:
            self.MainWin.CheckToResize('Dock2', 100)
        else:
            self.MainWin.CheckToResize('Dock2', self.width())

        self.MainWin.Dock2Free = not self.MainWin.Dock2Free

class ItemDsplyr(QTreeView):
    def __init__(self, CentrPane):
        QTreeView.__init__(self, CentrPane)
        self.CntrPane = CentrPane

        self.setEditTriggers(QTreeView().NoEditTriggers)
        self.model = CustomItemModel(0, 3)
        self.model.setHorizontalHeaderLabels(['1st Col', '2nd Col', '3rd Col'])
        self.model.setHeaderData(1, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole)
        
        self.setModel(self.model)
        self.setMinimumWidth(250)

        self.header().setStretchLastSection(False)
        self.header().setSectionResizeMode(2, QHeaderView.Stretch)               # <---------
        self.setColumnWidth(1, 75)
        self.setColumnWidth(2, 100)

class CenterPanel(QWidget):
    def __init__(self, MainWin):
        QWidget.__init__(self)

        self.MyEditor = QTextEdit('Editorial')
        self.ItemDsply = ItemDsplyr(self)

        CntrPane = QSplitter(Qt.Horizontal, self)
        CntrPane.addWidget(self.MyEditor)
        CntrPane.addWidget(self.ItemDsply)
        CntrPane.setSizes([50,200])
        CntrPane.setCollapsible(0, False)
        CntrPane.setCollapsible(1, False)

        hbox = QHBoxLayout(self)
        hbox.addWidget(CntrPane)

        self.setLayout(hbox)

class MenuToolBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
        self.MainWin = MainWin
        self.MainMenu = MainWin.menuBar()

        self.WndowMenu  = self.MainMenu.addMenu('Windows')

        self.Dock1Act = QAction('Dock1', self)
        self.Dock1Act.setStatusTip('Open the Dock1 Window')
        self.Dock1Act.triggered.connect(MainWin.ShowDock1)

        self.Dock2Act = QAction('Dock2', self)
        self.Dock2Act.setStatusTip('Open the Dock2 Window')
        self.Dock2Act.triggered.connect(MainWin.ShowDock2)

        self.WndowMenu.addAction(self.Dock1Act)
        self.WndowMenu.addSeparator()
        self.WndowMenu.addAction(self.Dock2Act)

        self.InitToolBar(MainWin)

    def InitToolBar(self, MainWin):
        self.mainToolBar = MainWin.addToolBar("Quick Access")

        self.mainToolBar.addAction(self.Dock1Act)
        self.mainToolBar.addSeparator()
        self.mainToolBar.addAction(self.Dock2Act)

class UI_MainWindow(QMainWindow):
    def __init__(self, MainDesktop):
        super(UI_MainWindow, self).__init__(MainDesktop)
        self.setWindowTitle('Main Window')
        self.Dock1Open = False
        self.Dock1Free = False
        self.Dock2Open = False
        self.Dock2Free = False

        self.MnDskTop = MainDesktop
        print(f"MainDesktop->{MainDesktop}")

      # Left, Top, Width, Height
        self.setGeometry(200, 100, 550, 550)

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.MenuToolBar = MenuToolBar(self)

    def ShowDock1(self):
        if not self.Dock1Open:
            self.CheckToResize('Dock1', 300)
            self.Dock1 = DockWin1(self)
            self.Dock1Open = True
            self.Dock1Free = False
            self.addDockWidget(Qt.RightDockWidgetArea, self.Dock1)

    def ClosingDock1(self):
        self.Dock1Open = False

    def ShowDock2(self):
        if not self.Dock2Open:
            self.CheckToResize('Dock2', 300)
            self.Dock2 = DockWin2(self)
            self.Dock2Open = True
            self.Dock2Free = False
            self.addDockWidget(Qt.RightDockWidgetArea, self.Dock2)

    def ClosingDock2(self):
        self.Dock2Open = False

    def CheckToResize(self, WinDocked, DockWdth):
      # If any of the other docks are open and docked do not resize
        if   self.Dock1Open and WinDocked != 'Dock1' and not self.Dock1Free:
            return
        elif self.Dock2Open and WinDocked != 'Dock2' and not self.Dock2Free:
            return

        Docking = True
        if   WinDocked == 'Dock1' and self.Dock1Open != self.Dock1Free:
            Docking = False
        elif WinDocked == 'Dock2'  and self.Dock2Open != self.Dock2Free:
            Docking = False

        WinLeft  = self.geometry().left()
        WinTop   = self.geometry().top()
        WinWidth = self.geometry().width()
        WinHight = self.geometry().height()
        ScrWidth = self.MnDskTop.screenGeometry().width()

      # Docking
        if Docking:
            if ScrWidth < (WinLeft + WinWidth + DockWdth):
                WinLeft  = 0
                WinWidth = ScrWidth
            elif WinLeft > (trunc(DockWdth/2)):
                WinLeft = WinLeft - trunc(DockWdth/2)
                WinWidth = WinWidth + DockWdth
            else:
                WinLeft = 0
                WinWidth = WinWidth + DockWdth

            self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
     # Un-Docking
        else:
            WinWidth = WinWidth - DockWdth
            self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
          # Call the Event that Triggers View Adjustment ?? 
            self.CenterPane.ItemDsply.updateGeometry()
#            self.CenterPane.ItemDsply.resizeEvent(QResizeEvent(self.size(), QSize()))

if __name__ == '__main__':
    MainApp = QApplication([])

    MainGui = UI_MainWindow(MainApp.desktop())
    MainGui.show()

    sysExit(MainApp.exec_())


更新

sizeAdjustPolicy : SizeAdjustPolicy

This property holds the policy describing how the size of the scroll area changes when the size of the viewport changes.

from sys  import exit as sysExit
from math import trunc

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

class CustomItemModel(QStandardItemModel):
    def headerData(self, section, orientation, role):
        if role == Qt.ForegroundRole:
            brush = QBrush()
            brush.setColor(Qt.blue)
            brush.setStyle(Qt.SolidPattern)
            return brush

        elif role == Qt.BackgroundRole:
            brush = QBrush()
            brush.setColor(Qt.yellow)
            brush.setStyle(Qt.SolidPattern)
            return brush

        elif role == Qt.FontRole:
            font = QFont()
            font.setBold(True)
            font.setPointSize(10)
            return font

        return super().headerData(section, orientation, role)

class DockWin1(QDockWidget):
    def __init__(self, parent):
        QDockWidget.__init__(self)

        self.setWindowTitle('Dock 1')
        self.MainWin = parent

        self.container = QWidget(self)
        self.container.setLayout(QHBoxLayout())
        self.setWidget(self.container)
        self.container.layout().addWidget(QTextEdit())
        self.setMinimumWidth(100)
        self.setMinimumHeight(100)

        self.topLevelChanged.connect(self.SetDock1Free)

    @property
    def MainWin(self):
        return self.__parent

    @MainWin.setter
    def MainWin(self, value):
        self.__parent = value

    def closeEvent(self, event):
        self.MainWin.ClosingDock1()

    def SetDock1Free(self):
        if self.MainWin.Dock1Free:
            self.MainWin.CheckToResize('Dock1', 100)
        else:
            self.MainWin.CheckToResize('Dock1', self.width())

        self.MainWin.Dock1Free = not self.MainWin.Dock1Free

class DockWin2(QDockWidget):
    def __init__(self, parent):
        QDockWidget.__init__(self)

        self.setWindowTitle('Dock 2')
        self.MainWin = parent

        self.container = QWidget(self)
        self.container.setLayout(QHBoxLayout())
        self.setWidget(self.container)
        self.container.layout().addWidget(QTextEdit())
        self.setMinimumWidth(100)
        self.setMinimumHeight(100)

        self.topLevelChanged.connect(self.SetDock2Free)

    @property
    def MainWin(self):
        return self.__parent

    @MainWin.setter
    def MainWin(self, value):
        self.__parent = value

    def closeEvent(self, event):
        self.MainWin.ClosingDock2()

    def SetDock2Free(self):
        if self.MainWin.Dock2Free:
            self.MainWin.CheckToResize('Dock2', 100)
        else:
            self.MainWin.CheckToResize('Dock2', self.width())

        self.MainWin.Dock2Free = not self.MainWin.Dock2Free

class ItemDsplyr(QTreeView):
    def __init__(self, CentrPane):
        QTreeView.__init__(self, CentrPane)
        self.CntrPane = CentrPane

        self.setEditTriggers(QTreeView().NoEditTriggers)
        self.model = CustomItemModel(0, 3)
        self.model.setHorizontalHeaderLabels(['1st Col', '2nd Col', '3rd Col'])
        self.model.setHeaderData(1, Qt.Horizontal, Qt.AlignCenter, Qt.TextAlignmentRole)
        
        self.setModel(self.model)
        self.setMinimumWidth(250)
        
        self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)     # <<<========

        self.header().setStretchLastSection(False)   
        self.header().setSectionResizeMode(0, QHeaderView.Stretch)               
        
        self.setColumnWidth(1, 75)
        self.setColumnWidth(2, 100)   

class CenterPanel(QWidget):
    def __init__(self, MainWin):
        QWidget.__init__(self)

        self.MyEditor = QTextEdit('Editorial')
        self.ItemDsply = ItemDsplyr(self)

        CntrPane = QSplitter(Qt.Horizontal, self)
        CntrPane.addWidget(self.MyEditor)
        CntrPane.addWidget(self.ItemDsply)
        CntrPane.setSizes([50,200])
        CntrPane.setCollapsible(0, False)
        CntrPane.setCollapsible(1, False)

        hbox = QHBoxLayout(self)
        hbox.addWidget(CntrPane)

        self.setLayout(hbox)

class MenuToolBar(QDockWidget):
    def __init__(self, MainWin):
        QDockWidget.__init__(self)
        self.MainWin = MainWin
        self.MainMenu = MainWin.menuBar()

        self.WndowMenu  = self.MainMenu.addMenu('Windows')

        self.Dock1Act = QAction('Dock1', self)
        self.Dock1Act.setStatusTip('Open the Dock1 Window')
        self.Dock1Act.triggered.connect(MainWin.ShowDock1)

        self.Dock2Act = QAction('Dock2', self)
        self.Dock2Act.setStatusTip('Open the Dock2 Window')
        self.Dock2Act.triggered.connect(MainWin.ShowDock2)

        self.WndowMenu.addAction(self.Dock1Act)
        self.WndowMenu.addSeparator()
        self.WndowMenu.addAction(self.Dock2Act)

        self.InitToolBar(MainWin)

    def InitToolBar(self, MainWin):
        self.mainToolBar = MainWin.addToolBar("Quick Access")

        self.mainToolBar.addAction(self.Dock1Act)
        self.mainToolBar.addSeparator()
        self.mainToolBar.addAction(self.Dock2Act)

class UI_MainWindow(QMainWindow):
    def __init__(self, MainDesktop):
        super(UI_MainWindow, self).__init__(MainDesktop)
        self.setWindowTitle('Main Window')
        self.Dock1Open = False
        self.Dock1Free = False
        self.Dock2Open = False
        self.Dock2Free = False

        self.MnDskTop = MainDesktop
#        print(f"MainDesktop->{MainDesktop}")

      # Left, Top, Width, Height
        self.setGeometry(200, 100, 550, 550)

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.MenuToolBar = MenuToolBar(self)

    def ShowDock1(self):
        if not self.Dock1Open:
            self.CheckToResize('Dock1', 300)
            self.Dock1 = DockWin1(self)
            self.Dock1Open = True
            self.Dock1Free = False
            self.addDockWidget(Qt.RightDockWidgetArea, self.Dock1)

    def ClosingDock1(self):
        self.Dock1Open = False

    def ShowDock2(self):
        if not self.Dock2Open:
            self.CheckToResize('Dock2', 300)
            self.Dock2 = DockWin2(self)
            self.Dock2Open = True
            self.Dock2Free = False
            self.addDockWidget(Qt.RightDockWidgetArea, self.Dock2)

    def ClosingDock2(self):
        self.Dock2Open = False

    def CheckToResize(self, WinDocked, DockWdth):
      # If any of the other docks are open and docked do not resize
        if   self.Dock1Open and WinDocked != 'Dock1' and not self.Dock1Free:
            return
        elif self.Dock2Open and WinDocked != 'Dock2' and not self.Dock2Free:
            return

        Docking = True
        if   WinDocked == 'Dock1' and self.Dock1Open != self.Dock1Free:
            Docking = False
        elif WinDocked == 'Dock2'  and self.Dock2Open != self.Dock2Free:
            Docking = False

        WinLeft  = self.geometry().left()
        WinTop   = self.geometry().top()
        WinWidth = self.geometry().width()
        WinHight = self.geometry().height()
        ScrWidth = self.MnDskTop.screenGeometry().width()

      # Docking
        if Docking:
            if ScrWidth < (WinLeft + WinWidth + DockWdth):
                WinLeft  = 0
                WinWidth = ScrWidth
            elif WinLeft > (trunc(DockWdth/2)):
                WinLeft = WinLeft - trunc(DockWdth/2)
                WinWidth = WinWidth + DockWdth
            else:
                WinLeft = 0
                WinWidth = WinWidth + DockWdth

            self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
     # Un-Docking
        else:
            WinWidth = WinWidth - DockWdth
            self.setGeometry(WinLeft, WinTop, WinWidth, WinHight)
          # Call the Event that Triggers View Adjustment ?? 
            self.CenterPane.ItemDsply.updateGeometry()
#            self.CenterPane.ItemDsply.resizeEvent(QResizeEvent(self.size(), QSize()))

if __name__ == '__main__':
    MainApp = QApplication([])

    MainGui = UI_MainWindow(MainApp.desktop())
    MainGui.show()

    sysExit(MainApp.exec_())

好吧,它最终实际上是@S 的组合。尼克说(上)

self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)

需要添加到 ItemDsplyr class

然而,仍然需要一些东西来触发调整大小,因为当使用 setGeometry() 时 Dock Window 分离时它并不总是触发,所以我用 resize() 替换了 setGeometry() 如下:

WinWidth = WinWidth - DockWdth
self.resize(WinWidth, WinHight)

请注意,如果我需要在取消停靠时重新定位 window,那么我只需要调用 setGeometry(),然后调用 resize(),因为看起来 resize() 是触发重绘功能的原因