如何在使用 QSplitter 时固定一个小部件
How to make one widget fixed when using QSplitter
共有三个框架:顶部、中部和底部。每当我调整底部框架的大小时,顶部框架总是同时调整大小。当底部框架调整大小时,我希望顶部框架保持其大小。如何实现?
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
top = QFrame(self)
top.setFrameShape(QFrame.StyledPanel)
middle = QFrame(self)
middle.setFrameShape(QFrame.StyledPanel)
bottom = QFrame(self)
bottom.setFrameShape(QFrame.StyledPanel)
splitter1 = QSplitter(Qt.Vertical)
splitter1.addWidget(top)
splitter1.addWidget(middle)
splitter1.setSizes([300,300])
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
splitter2.setSizes([600,400])
hbox.addWidget(splitter2)
self.setLayout(hbox)
self.setGeometry(300, 300, 1000, 1000)
self.setWindowTitle('QSplitter')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
如果您希望所有帧彼此独立调整大小,请将它们全部放在同一个拆分器中,如下所示:
def initUI(self):
hbox = QHBoxLayout(self)
top = QFrame(self)
top.setFrameShape(QFrame.StyledPanel)
middle = QFrame(self)
middle.setFrameShape(QFrame.StyledPanel)
bottom = QFrame(self)
bottom.setFrameShape(QFrame.StyledPanel)
splitter1 = QSplitter(Qt.Vertical)
splitter1.addWidget(top)
splitter1.addWidget(middle)
splitter1.addWidget(bottom)
splitter1.setSizes([500,300,200])
hbox.addWidget(splitter1)
self.setLayout(hbox)
共有三个框架:顶部、中部和底部。每当我调整底部框架的大小时,顶部框架总是同时调整大小。当底部框架调整大小时,我希望顶部框架保持其大小。如何实现?
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
top = QFrame(self)
top.setFrameShape(QFrame.StyledPanel)
middle = QFrame(self)
middle.setFrameShape(QFrame.StyledPanel)
bottom = QFrame(self)
bottom.setFrameShape(QFrame.StyledPanel)
splitter1 = QSplitter(Qt.Vertical)
splitter1.addWidget(top)
splitter1.addWidget(middle)
splitter1.setSizes([300,300])
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)
splitter2.addWidget(bottom)
splitter2.setSizes([600,400])
hbox.addWidget(splitter2)
self.setLayout(hbox)
self.setGeometry(300, 300, 1000, 1000)
self.setWindowTitle('QSplitter')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
如果您希望所有帧彼此独立调整大小,请将它们全部放在同一个拆分器中,如下所示:
def initUI(self):
hbox = QHBoxLayout(self)
top = QFrame(self)
top.setFrameShape(QFrame.StyledPanel)
middle = QFrame(self)
middle.setFrameShape(QFrame.StyledPanel)
bottom = QFrame(self)
bottom.setFrameShape(QFrame.StyledPanel)
splitter1 = QSplitter(Qt.Vertical)
splitter1.addWidget(top)
splitter1.addWidget(middle)
splitter1.addWidget(bottom)
splitter1.setSizes([500,300,200])
hbox.addWidget(splitter1)
self.setLayout(hbox)