I created a vertical layout and placed two buttons inside. After the program runs, the mouse drags the window to change its size.
The vertical distance between the two buttons or the length of the button changes.
However, if this vertical layout is nested outside another vertical layout, changing the window does not change the height of the inner vertical layout, ie. the vertical distance of the two buttons does not change (the width can be changed), why? A relatively complex layout may require this multi-level layout nesting, how to solve it? Thank you!

The following code has only one vertical layout and work normally
Qt Code:
  1. import sys
  2. from PyQt5.QtWidgets import *
  3.  
  4. class MyWindow(QWidget):
  5. def __init__(self):
  6. super(MyWindow, self).__init__()
  7. self.setGeometry(300, 300, 300, 200)
  8. self.setWindowTitle('test vbox ok')
  9.  
  10. whole_layout=QHBoxLayout()
  11.  
  12. vlayout=QVBoxLayout()
  13. vlayout.addWidget(QPushButton(str(3)))
  14. vlayout.addWidget(QPushButton(str(4)))
  15.  
  16. vwg=QWidget()
  17. vwg.setLayout(vlayout)
  18.  
  19. whole_layout.addWidget(vwg)
  20. self.setLayout(whole_layout)
  21.  
  22. if __name__ == '__main__':
  23. app=QApplication(sys.argv)
  24. win=MyWindow()
  25. win.show()
  26. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode 


The following code hase two vertical layout nested, work abnormally
Qt Code:
  1. import sys
  2. from PyQt5.QtWidgets import *
  3.  
  4. class MyWindow(QWidget):
  5. def __init__(self):
  6. super(MyWindow, self).__init__()
  7. self.setGeometry(300, 300, 300, 200)
  8. self.setWindowTitle('test vbox fail')
  9.  
  10. whole_layout=QHBoxLayout()
  11.  
  12. vlayout=QVBoxLayout()
  13. vlayout2=QVBoxLayout()
  14.  
  15. vlayout.addWidget(QPushButton(str(3)))
  16. vlayout.addWidget(QPushButton(str(4)))
  17.  
  18. vwg=QWidget()
  19. # two vertical layout netsed
  20. vlayout2.addLayout(vlayout)
  21. vwg.setLayout(vlayout2)
  22.  
  23. whole_layout.addWidget(vwg)
  24. self.setLayout(whole_layout)
  25.  
  26. if __name__ == '__main__':
  27. app=QApplication(sys.argv)
  28. win=MyWindow()
  29. win.show()
  30. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode