- PyQt5 Tutorial
- PyQt5 - Home
- PyQt5 - Introduction
- PyQt5 - What’s New
- PyQt5 - Hello World
- PyQt5 - Major Classes
- PyQt5 - Using Qt Designer
- PyQt5 - Signals & Slots
- PyQt5 - Layout Management
- PyQt5 - Basic Widgets
- PyQt5 - QDialog Class
- PyQt5 - QMessageBox
- PyQt5 - Multiple Document Interface
- PyQt5 - Drag & Drop
- PyQt5 - Database Handling
- PyQt5 - Drawing API
- PyQt5 - BrushStyle Constants
- PyQt5 - QClipboard
- PyQt5 - QPixmap Class
- PyQt5 Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - Useful Resources
- PyQt5 - Discussion
PyQt5 - QFormLayout Class
QFormLayout is a convenient way to create two column form, where each row consists of an input field associated with a label. As a convention, the left column contains the label and the right column contains an input field. Mainly three overloads of addRow() method addLayout() are commonly used.
Sr.No. | Methods & Description |
---|---|
1 |
addRow(QLabel, QWidget) Adds a row containing label and input field |
2 |
addRow(QLabel, QLayout) Adds a child layout in the second column |
3 |
addRow(QWidget) Adds a widget spanning both columns |
Example
This code adds a LineEdit field to input name in the first row. Then it adds a vertical box layout for two address fields in the second column of the next row. Next, a horizontal box layout object containing two Radio button fields is added in the second column of the third row. The fourth row shows two buttons ‘Submit’ and ‘Cancel’.
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * def window(): app = QApplication(sys.argv) win = QWidget() l1 = QLabel("Name") nm = QLineEdit() l2 = QLabel("Address") add1 = QLineEdit() add2 = QLineEdit() fbox = QFormLayout() fbox.addRow(l1,nm) vbox = QVBoxLayout() vbox.addWidget(add1) vbox.addWidget(add2) fbox.addRow(l2,vbox) hbox = QHBoxLayout() r1 = QRadioButton("Male") r2 = QRadioButton("Female") hbox.addWidget(r1) hbox.addWidget(r2) hbox.addStretch() fbox.addRow(QLabel("sex"),hbox) fbox.addRow(QPushButton("Submit"),QPushButton("Cancel")) win.setLayout(fbox) win.setWindowTitle("PyQt") win.show() sys.exit(app.exec_()) if __name__ == '__main__': window()
The above code produces the following output −