PyQt5 Quick Start (five) PyQt5 layout manager
A, GUI layout manager Profile
A method for managing PyQt5 interface layout consists essentially of two, one is absolute positioning, it is to use a layout manager. After Qt layout using absolute positioning can not adaptively change window, Qt thus provides the interface to the class of components layout manager, for managing the interface component, the interface component can be automatically aligned window, window size change automatically update the size of the interface components.
QLayout Qt is the abstract base class layout manager, through inheritance of QLayout achieve the different functions and complementary layout manager. Layout manager interface assembly is not, but positioning strategy interface components; type of any container assembly can specify a layout manager; component manager with a layout management have the same parent component, provided it has received the layout manager implicitly specifies the parent-child relationship.
Qt layout manager for inserting addWidget to control the layout manager, addLayout a layout manager inserts the layout manager.
Second, the box layout
1、QHBoxLayout
QHBoxLayout provides a way to control the level layout management.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QSizePolicy
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QHBoxLayout()
self.layout.setSpacing(20)
button = QPushButton("Button1")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.layout.addWidget(button)
button = QPushButton("Button2")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.layout.addWidget(button)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
2、QVBoxLayout
QVBoxLayout provides a way to control the vertical layout management.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QSizePolicy
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QVBoxLayout()
self.layout.setSpacing(20)
button = QPushButton("Button1")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.layout.addWidget(button)
button = QPushButton("Button2")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.layout.addWidget(button)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
3, nested layout
Layout Manager can be nested with each other to form a complex layout management. QBoxLayout nested layout manager instance as follows:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QSizePolicy
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QVBoxLayout()
self.layout.setSpacing(20)
# 第一行按钮布局管理
hLayout1 = QHBoxLayout()
button = QPushButton("Button1")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
hLayout1.addWidget(button)
button = QPushButton("Button2")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
hLayout1.addWidget(button)
# 第二行按钮布局管理
hLayout2 = QHBoxLayout()
button = QPushButton("Button1")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
hLayout2.addWidget(button)
button = QPushButton("Button2")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
hLayout2.addWidget(button)
# 整体垂直布局管理
self.layout.addLayout(hLayout1)
self.layout.addLayout(hLayout2)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
Third, grid layout
QGridLayout grid layout manager to manage the interface component.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QSizePolicy
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QGridLayout()
self.layout.setSpacing(5)
button = QPushButton("Button1")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
self.layout.addWidget(button, 0, 0, 1, 1)
button = QPushButton("Button2")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
self.layout.addWidget(button, 0, 1, 1, 1)
button = QPushButton("Button3")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
self.layout.addWidget(button, 1, 0, 1, 1)
button = QPushButton("Button4")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
self.layout.addWidget(button, 1, 1, 1, 1)
button = QPushButton("Button5")
button.setMinimumSize(60, 30)
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# 列扩展,定位在第2行第1列位置,占1行2列
self.layout.addWidget(button, 2, 0, 1, 2)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
Fourth, form layout
QFormLayout layout manager uses the management of interface components form, the form and label components are mutually corresponding relationship, support nest.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QVBoxLayout, QLineEdit
from PyQt5.QtCore import Qt
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QFormLayout()
self.layout.setSpacing(20)
self.layout.setLabelAlignment(Qt.AlignLeft)
self.layout.setFormAlignment(Qt.AlignRight)
nameEdit = QLineEdit()
mailEdit = QLineEdit()
vLayout = QVBoxLayout()
vLayout.setSpacing(6)
addrEdit1 = QLineEdit()
addrEdit2 = QLineEdit()
vLayout.addWidget(addrEdit1)
vLayout.addWidget(addrEdit2)
self.layout.addRow("Name:", nameEdit)
self.layout.addRow("Mail:", mailEdit)
self.layout.addRow("Address:", vLayout)
self.setLayout(self.layout)
self.setWindowTitle("FTP")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
Fifth, the stack layout
All components QStackedLayout stacker Layout Management in a direction perpendicular to the screen, only one interface component displayed on the screen, as long as the top level interface components will be displayed.
Features QStackedLayout stack layout manager follows:
A, components the same size, and the full display area of the parent component
B, the other can not be directly nested layout manager
C, the switch assembly can freely be displayed
D, will only show a interface components
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QStackedLayout, QVBoxLayout, QPushButton
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QStackedLayout()
self.layout.addWidget(QPushButton("Button1"))
self.layout.addWidget(QPushButton("Button2"))
self.layout.addWidget(QPushButton("Button3"))
self.layout.addWidget(QPushButton("Button4"))
self.setLayout(self.layout)
self.setWindowTitle("Stack Layout")
# 设置栈顶显示第2个组件
self.layout.setCurrentIndex(2)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
QStackedLayout stack layout manager can not directly be nested other layout manager, but may be nested with other indirectly by the layout manager QWidget container assembly.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QStackedLayout, QVBoxLayout, QPushButton
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QStackedLayout()
self.layout.addWidget(QPushButton("Button1"))
# 容器型组件
widget = QWidget()
vLayout = QVBoxLayout()
vLayout.addWidget(QPushButton("Button2"))
vLayout.addWidget(QPushButton("Button3"))
widget.setLayout(vLayout)
self.layout.addWidget(widget)
self.layout.addWidget(QPushButton("Button4"))
self.setLayout(self.layout)
self.setWindowTitle("Stack Layout")
# 设置栈顶显示第1个组件
self.layout.setCurrentIndex(1)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())
Sixth, splitter
QSplitter is cut with a stripe (splitter handle) The layout manager may be set by the Segmentation of the broadband setHandleWidth () function. QSplitter can be specified by dividing the setOrientation direction () function, the sub-components arranged in a specified direction load order.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QSplitter
from PyQt5.QtCore import Qt
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QVBoxLayout()
self.mainSplitter = QSplitter(self)
self.layout.addWidget(self.mainSplitter)
self.setLayout(self.layout)
# 水平线分割
self.mainSplitter.setOrientation(Qt.Horizontal)
rightSplitter = QSplitter(self)
# 垂直线分割
rightSplitter.setOrientation(Qt.Vertical)
textEdit = QTextEdit()
textEdit.setText("Window2")
rightSplitter.addWidget(textEdit)
textEdit = QTextEdit()
textEdit.setText("Window3")
rightSplitter.addWidget(textEdit)
textEdit = QTextEdit()
textEdit.setText("Window1")
self.mainSplitter.addWidget(textEdit)
self.mainSplitter.addWidget(rightSplitter)
# 分割比例
self.mainSplitter.setStretchFactor(0, 1)
self.mainSplitter.setStretchFactor(1, 2)
self.mainSplitter.show()
self.setWindowTitle("Splitter")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.resize(400, 200)
window.show()
sys.exit(app.exec_())