PyQt - 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.

Following is the 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

Text Container and Radio Button in PyQt

The method LineEdit() create a single-line text-entry widget container whereas QRadioButton() create the option selection. Here, below the program build GUI form using following components- widgets using QLabel(), radio button using QRadioButton(), and QPushButton() to create the clickable button.

Example

In this example, the program 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 PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.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)

   # Create a button group for the radio buttons
   gender_group = QButtonGroup()

   r1 = QRadioButton("Male")
   r2 = QRadioButton("Female")

   # Add the radio buttons to the group
   gender_group.addButton(r1)
   gender_group.addButton(r2)

   hbox = QHBoxLayout()
   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()

Output

The above code produces the following output −

qformlayout Example One

Sign Up Form using QFormLayout class

Every website builds a sign-up form to store the data. It enables the user to register the form from multiple locations with the information. Here, we use the addrow() method to add the new label to sign-up page and display the result using show() method.

Example

In this example, we illustrate the sign up form using the class QFormLayout.

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit,  QFormLayout
class MainWindow(QWidget):
   def __init__(self, *args, **kwargs):
      super().__init__(*args, **kwargs)

      self.setWindowTitle('Sign Up Form')

      layout = QFormLayout()
      self.setLayout(layout)

      layout.addRow('Enter the name:', QLineEdit(self))
      layout.addRow('Enter the Email:', QLineEdit(self))
      layout.addRow('Enter the Pincode:', QLineEdit(self, echoMode=QLineEdit.EchoMode.Password))
      layout.addRow('Enter the Mobile Number:', QLineEdit(self))
      layout.addRow(QPushButton('Sign Up Form'))

      # show the window
      self.show()
if __name__ == '__main__':
   app = QApplication(sys.argv)
   window = MainWindow()
   sys.exit(app.exec())

Output

On executing the code, we get the result of sign up form in PyQt Window-

Sign Up Form
Advertisements