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
import sys
from PyQt5.QtWidgets import *
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('test vbox ok')
vwg.setLayout(vlayout)
whole_layout.addWidget(vwg)
self.setLayout(whole_layout)
if __name__ == '__main__':
win=MyWindow()
win.show()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('test vbox ok')
whole_layout=QHBoxLayout()
vlayout=QVBoxLayout()
vlayout.addWidget(QPushButton(str(3)))
vlayout.addWidget(QPushButton(str(4)))
vwg=QWidget()
vwg.setLayout(vlayout)
whole_layout.addWidget(vwg)
self.setLayout(whole_layout)
if __name__ == '__main__':
app=QApplication(sys.argv)
win=MyWindow()
win.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
The following code hase two vertical layout nested, work abnormally
import sys
from PyQt5.QtWidgets import *
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('test vbox fail')
# two vertical layout netsed
vlayout2.addLayout(vlayout)
vwg.setLayout(vlayout2)
whole_layout.addWidget(vwg)
self.setLayout(whole_layout)
if __name__ == '__main__':
win=MyWindow()
win.show()
sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import *
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('test vbox fail')
whole_layout=QHBoxLayout()
vlayout=QVBoxLayout()
vlayout2=QVBoxLayout()
vlayout.addWidget(QPushButton(str(3)))
vlayout.addWidget(QPushButton(str(4)))
vwg=QWidget()
# two vertical layout netsed
vlayout2.addLayout(vlayout)
vwg.setLayout(vlayout2)
whole_layout.addWidget(vwg)
self.setLayout(whole_layout)
if __name__ == '__main__':
app=QApplication(sys.argv)
win=MyWindow()
win.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks