Don Smythe
Don Smythe

Reputation: 9804

PyQt QSplitter setSizes usage

I am using a QSplitter and would like to set the initial relative sizes of the panes in the splitter eg in a 1 to 10 ratio. However, the ratio will never be less than 76 to 100, no matter what size I set the window to. Any ideas?

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()    

Upvotes: 5

Views: 10643

Answers (1)

Don Smythe
Don Smythe

Reputation: 9804

Instead of using setSizes() use setStretchfactor() as per below:

splitter.setStretchFactor(1, 10)

The setSizes() method is absolute not relative, it sets the sizes to actual pixel sizes - hence why trying to set the size of a widget to 10 pixels didn't work.

Upvotes: 9

Related Questions