gpt4 book ai didi

python - 如何在使用 QSplitter 时固定一个小部件

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:46 25 4
gpt4 key购买 nike

共有三个框架:顶部、中间和底部。每当我调整底部框架的大小时,顶部框架总是同时调整大小。我希望当底部框架调整大小时顶部框架保持其大小。如何实现这一目标?

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)

关于python - 如何在使用 QSplitter 时固定一个小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47836001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com