当 window 调整大小时,PyQt5 仅调整一个子部件的大小

PyQt5 resize only one child widget when window is resized

我正在 PyQt5 中构建 GUI。它的结构如附图所示。

我需要的是在 window 调整大小时进行管理:

这里是这个例子的代码:

def initUI(self):  

    topHBoxLayout = QHBoxLayout(self)
    topHBoxLayout.setContentsMargins(0,0,0,0) 

    sstFrame  = QFrame() 
    sstFrame.setMinimumSize(QSize(100, 100))
    self.colorFrame(sstFrame, 'red')

    objectivesFrame = QFrame() 
    objectivesFrame.setMinimumSize(QSize(100, 100))
    self.colorFrame(objectivesFrame, 'green')

    wsTop = QFrame() 
    wsTop.setMinimumSize(QSize(100, 100))
    self.colorFrame(wsTop, 'blue')

    wsBottom = QFrame() 
    wsBottom.setMinimumSize(QSize(100, 100))
    self.colorFrame(wsBottom, 'purple')

    workspaceSplitter = QSplitter(Qt.Vertical) 
    workspaceSplitter.addWidget(wsTop)
    workspaceSplitter.addWidget(wsBottom)


    mainSplitter = QSplitter(Qt.Horizontal)
    mainSplitter.addWidget(sstFrame)
    mainSplitter.addWidget(objectivesFrame)
    mainSplitter.addWidget(workspaceSplitter)

    topHBoxLayout.addWidget(mainSplitter)   

    self.setLayout(topHBoxLayout)

    self.setGeometry(300, 300, 300, 200)
    self.setWindowTitle('')
    self.show()

感谢您的建议,我自己也想不通。

无论我尝试什么,我都无法使附加到单个拆分器实例的三个小部件发挥任何作用(我尝试了 SizePolicy 设置、QSplitter 拉伸因子,...)。

因此尝试用更多的 QFrames 包装它并且似乎有效。但我仍然对原始代码的解决方案持开放态度。

这是我的结果:

def initUI(self):  

    topHBoxLayout = QHBoxLayout(self)
    topHBoxLayout.setContentsMargins(0,0,0,0)

    sstObjWrapper = QSplitter(Qt.Horizontal) 
    sstObjWrapper.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

    sstFrame  = QFrame() 
    sstFrame.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
    sstFrame.setMinimumSize(QSize(100, 100))
    self.colorFrame(sstFrame, 'red') 

    # --- sst frame ---

    objectivesFrame = QFrame() 
    objectivesFrame.setMinimumSize(QSize(100, 100))
    objectivesFrame.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
    self.colorFrame(objectivesFrame, 'green')

    sstObjWrapper.addWidget(sstFrame)
    sstObjWrapper.addWidget(objectivesFrame)

    tmp1  = QFrame(self)
    tmp1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    tmp1.setMinimumSize(QSize(200, 200))
    self.colorFrame(tmp1, 'purple') 

    tmp2  = QFrame(self) 
    tmp2.setMinimumSize(QSize(100, 100))
    tmp2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
    self.colorFrame(tmp2, 'blue') 

    plotConsoleSplitter = QSplitter(Qt.Vertical)
    plotConsoleSplitter.addWidget(tmp1)
    plotConsoleSplitter.addWidget(tmp2)
    plotConsoleSplitter.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)


    objPlotSplitter = QSplitter(Qt.Horizontal)
    objPlotSplitter.addWidget(sstObjWrapper)
    objPlotSplitter.addWidget(plotConsoleSplitter)

    objPlotSplitter.setStretchFactor ( 0, 0 )
    objPlotSplitter.setStretchFactor ( 1, 1 )


    topHBoxLayout.addWidget(objPlotSplitter)

    self.setLayout(topHBoxLayout)
    self.setGeometry(300, 300, 300, 200)
    self.setWindowTitle('')
    self.show()