Python PyQt Layout Management

In GUI application development,layout managementrefers to how to organize and arrange various controls (such as buttons, text boxes, etc.) in a window.

PyQt provides multiple layout managers that can help us automatically adjust the position and size of controls, so that the interface can maintain a good display effect under windows of different sizes.

Why Layout Management is Needed

  1. Adapt to different resolutions: Users may use screens of different sizes
  2. Simplify development: No need to manually calculate the position of each control
  3. Easy maintenance: When adding or removing controls, the layout adjusts automatically

PyQt's Main Layout Managers

PyQt provides the following commonly used layout managers:

1. QHBoxLayout - Horizontal Layout

Arranges controls horizontally from left to right.

Example

layout = QHBoxLayout()
layout.addWidget(QPushButton("Button 1"))
layout.addWidget(QPushButton("Button 2"))
layout.addWidget(QPushButton("Button 3"))

2. QVBoxLayout - Vertical Layout

Arranges controls vertically from top to bottom.

Example

layout = QVBoxLayout()
layout.addWidget(QPushButton("Button 1"))
layout.addWidget(QPushButton("Button 2"))
layout.addWidget(QPushButton("Button 3"))

3. QGridLayout - Grid Layout

Arranges controls in rows and columns of a grid.

Example

layout = QGridLayout()
layout.addWidget(QPushButton("1,1"), 0, 0)  # Row 0, Column 0
layout.addWidget(QPushButton("1,2"), 0, 1)  # Row 0, Column 1
layout.addWidget(QPushButton("2,1"), 1, 0)  # Row 1, Column 0

4. QFormLayout - Form Layout

Especially suitable for creating form interfaces with label-field pairs.

Example

layout = QFormLayout()
layout.addRow("Name:", QLineEdit())
layout.addRow("Age:", QLineEdit())

Nested Use of Layouts

In practical applications, we often need to combine multiple layout managers to create complex interfaces.

Example: Main Window Layout

Example

# Create the main window
window = QWidget()

# Create the main vertical layout
main_layout = QVBoxLayout()

# Add a top horizontal layout
top_layout = QHBoxLayout()
top_layout.addWidget(QPushButton("File"))
top_layout.addWidget(QPushButton("Edit"))
main_layout.addLayout(top_layout)

# Add a central grid layout
center_layout = QGridLayout()
center_layout.addWidget(QTextEdit(), 0, 0, 2, 2)  # Occupies 2 rows and 2 columns
center_layout.addWidget(QPushButton("Save"), 0, 2)
center_layout.addWidget(QPushButton("Cancel"), 1, 2)
main_layout.addLayout(center_layout)

# Add a bottom horizontal layout
bottom_layout = QHBoxLayout()
bottom_layout.addStretch()  # Add stretchable space
bottom_layout.addWidget(QPushButton("OK"))
bottom_layout.addWidget(QPushButton("Cancel"))
main_layout.addLayout(bottom_layout)

window.setLayout(main_layout)
window.show()

Advanced Features of Layouts

1. Stretch Factor

Can control the relative size of controls in the layout.

Example

layout = QHBoxLayout()
layout.addWidget(QPushButton("Left"), stretch=1)  # Occupies 1 part of the space
layout.addWidget(QPushButton("Center"), stretch=2)  # Occupies 2 parts of the space
layout.addWidget(QPushButton("Right"), stretch=1)  # Occupies 1 part of the space

2. Spacing Control

Example

layout.setSpacing(10)  # Spacing between controls
layout.setContentsMargins(20, 20, 20, 20)  # Margins at the edges of the layout

3. Alignment

Example

button = QPushButton("Button")
layout.addWidget(button, alignment=Qt.AlignRight)  # Right-aligned

Best Practices

  1. Prioritize using layout managers: Avoid using absolute positioning (setGeometry)
  2. Reasonably nest layouts: Combine different layouts to create complex interfaces
  3. Use stretchable space: Make the interface display well at different sizes
  4. Maintain consistency: Use the same layout approach for similar interface elements
  5. Test different resolutions: Ensure the interface works properly on various screen sizes

By mastering PyQt's layout management system, you can create professional, beautiful, and highly adaptable GUI application interfaces.

Other Extensions