I am using QSplitter in my project, and I want to set the stretch factor for two widget. The following code is recommanded:
splitter.setStretchFactor(0, 1)
splitter.setStretchFactor(1, 5)
And I need to firstly hide one widget, then show it after I click a button. I find the stretch factor does not work. The whole code is:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class MainWindow(QWidget):
def __init__(self):
super().__init__()
mainLayout = QVBoxLayout()
self.setLayout(mainLayout)
self.btn = QPushButton('show')
self.btn.clicked.connect(self.btnSlot)
mainLayout.addWidget(self.btn)
layout = QHBoxLayout()
mainLayout.addLayout(layout)
self.w1 = QWidget()
self.w1.setStyleSheet('border: 2px solid #777;')
self.w1.hide()
self.lay1 = QVBoxLayout()
self.lay1.addWidget(QLabel('label 1'))
self.w1.setLayout(self.lay1)
w2 = QWidget()
w2.setStyleSheet('border: 2px solid red;')
self.lay2 = QVBoxLayout()
w2.setLayout(self.lay2)
splitter = QSplitter()
splitter.addWidget(self.w1)
splitter.addWidget(w2)
splitter.setStretchFactor(0, 1)
splitter.setStretchFactor(1, 5)
layout.addWidget(splitter)
def btnSlot(self, check=False):
self.w1.show()
self.lay2.addWidget(QLabel('label 2'))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
After clicked the 'show' button, the result is:
From the above figure, we can see the stretch factor for widget 1/2 is not 1:5. How can I make the strech factor to be 1:5?
Any suggestion is appreciated!

