我正在使用一个QSplitter,并希望设置初始相对大小的窗格在拆分器eg在1比10的比率但是,无论我将窗口设置为多大,比率永远不会小于76:100有什么想法吗?
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QScrollArea
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
first = QtGui.QFrame(self)
first.setFrameShape(QtGui.QFrame.StyledPanel)
scrollAreaLeft = QScrollArea()
scrollAreaLeft.setWidgetResizable(True)
scrollAreaLeft.setWidget(first)
second = QtGui.QFrame(self)
second.setFrameShape(QtGui.QFrame.StyledPanel)
scrollAreaRight = QScrollArea()
scrollAreaRight.setWidgetResizable(True)
scrollAreaRight.setWidget(second)
splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
splitter.addWidget(scrollAreaLeft)
splitter.addWidget(scrollAreaRight)
splitter.setSizes([10, 100])
hbox.addWidget(splitter)
self.setLayout(hbox)
self.setGeometry(600, 600, 600, 600)
self.setWindowTitle('QtGui.QSplitter')
self.show()
print ("scrollAreaLeft width: "+str(scrollAreaLeft.width()))
print ("scrollAreaRight width: "+str(scrollAreaRight.width()))
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
最佳答案
使用setStretchFactor()而不是setSizes(),如下所示:
splitter.setStretchFactor(1, 10)
setSizes()方法是绝对的,而不是相对的,它将大小设置为实际的像素大小-因此,尝试将小部件的大小设置为10像素是行不通的。
关于python - PyQt QSplitter setSizes用法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29537762/