当 window 调整大小时,PyQt5 仅调整一个子部件的大小
PyQt5 resize only one child widget when window is resized
我正在 PyQt5 中构建 GUI。它的结构如附图所示。
我需要的是在 window 调整大小时进行管理:
- 蓝色区域水平和垂直调整大小
- 阅读和绿色区域仅垂直调整大小
- 紫色区域仅水平调整大小
- 用户可以根据需要手动更改小部件的大小,但是当整个 window 调整大小时应该保持其 width/heigh
这里是这个例子的代码:
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()
我正在 PyQt5 中构建 GUI。它的结构如附图所示。
- 蓝色区域水平和垂直调整大小
- 阅读和绿色区域仅垂直调整大小
- 紫色区域仅水平调整大小
- 用户可以根据需要手动更改小部件的大小,但是当整个 window 调整大小时应该保持其 width/heigh
这里是这个例子的代码:
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()