取消对接后调整几何形状后如何触发QTreeView的更新

时间:2019-06-19 15:16:02

标签: python pyqt pyqt5 python-3.7 qtreeview

使用: Python 3.7 pyqt5 Windows10

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

我尝试了以下方法:

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适当地调整为新调整大小的窗口,以使其显示所有三列。目前,它会将最后一个推到列之外。

请求的图像 :(请参阅评论)

DockWidget Attached Immediately After Detaching After Tweaking the Main Window a Smidge

2 个答案:

答案 0 :(得分:1)

  

void QHeaderView :: setSectionResizeMode(int logicalIndex,QHeaderView :: ResizeMode模式)

     

设置约束,以限制标头中由logicIndex指定的部分如何调整为给定模式描述的大小。逻辑索引应在调用此函数时存在。

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

enter image description here


更新

  

sizeAdjustPolicy:SizeAdjustPolicy

     

此属性保留用于描述视口大小更改时滚动区域大小如何更改的策略。

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

enter image description here

答案 1 :(得分:0)

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

self.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)

需要添加到ItemDsplyr类

但是仍然需要一些触发大小调整的方法,因为当使用setGeometry()分离Dock窗口时,它并不总是触发,因此我用resize()替换了setGeometry(),如下所示:

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

请注意,如果我需要在停靠时重新定位窗口,那么我只需要先调用setGeometry(),再调用resize()即可,因为看起来resize()会触发重新绘制功能