SoFunction
Updated on 2024-11-16

python GUI library graphical interface development PyQt5 form layout control QFormLayout detailed usage and examples

Introduction to the PyQt5 layout control QFormLayout

QFormLayout is a label-field type of form layout, as I thought, is to realize the layout of the form way, the form is a mode of prompting the user to interact with, there are mainly two columns, the first column is used to display the information to the user prompts, generally known as the label field, the second column needs to be selected by the user or input, generally known as the field field, the relationship between the label and field is the label associated with the field, the label is the field. The relationship between label and field is that label is associated with field.

Example of using QFormLayout form layout

import sys
from  import QApplication ,QWidget ,QFormLayout , QLineEdit, QLabel
class Winform(QWidget):
  def __init__(self,parent=None):
    super(Winform,self).__init__(parent)
    ("Forms Layout Management Example") 
    (400, 100) 
    fromlayout = QFormLayout()
    labl1 = QLabel("Label 1")
    lineEdit1 = QLineEdit()
    labl2 = QLabel("Label 2")
    lineEdit2 = QLineEdit()
    labl3 = QLabel("Label 3")
    lineEdit3 = QLineEdit()
    (labl1, lineEdit1)
    (labl2, lineEdit2)
    (labl3, lineEdit3)
    //fromlayout->setRowWrapPolicy(QFormLayout::WrapLongRows);//this parameter is usually used in small screens, when the label and text box are not displayed in this line, the text box will be displayed in the next line, making the label exclusive to one line.
    fromlayout->setLabelAlignment(Qt::AlignLeft);// Set the alignment of the label
    (fromlayout)  
if __name__ == "__main__": 
    app = QApplication() 
    form = Winform()
    ()
    (app.exec_())

Schematic diagram of operation effect

The setRowWrapPolicy(RowWrapPolicy policy) function has three optional arguments:

QFormLayout::DontWrapRows: the meaning of this parameter is that the textbox always appears behind the label, where the label is given enough horizontal space to fit the widest label appearing in the form and the rest of the space is given to the textbox.

QFormLayout::WrapLongRows: this parameter is usually used in small screen, when the label and the text box are not displayed in the current line of the screen, the text box will be displayed in the next line, so that the label occupies one line.

QFormLayout::WrapAllRows: this parameter indicates that the label is always on the top line of the textbox.

The corresponding results, respectively, are as follows:

QFormLayout Layout Manager Nesting

According to the parameters of the AddRow function to see us in addition to adding form items can add Widget components can also add other layout managers.

QFormLayout* layout = new QFormLayout();//Top Layout Manager
QVBoxLayout* vlayout = new QVBoxLayout();// Layout managers that are used in a nested way
QLineEdit* text1 = new QLineEdit();
QLineEdit* text2 = new QLineEdit();
QLineEdit* text3 = new QLineEdit();
vlayout->addWidget(text2);// Sub-manager layout
vlayout->addWidget(text3);
layout->addRow("Name:",text1);//Add table entries to the QFormLayout.
layout->addRow("Tel:",vlayout);
layout->setSpacing(10);
setLayout(layout);// Setting up the top-level layout manager
this->setWindowTitle("FTP");

The output result is:

wrap-up

  • QFormLayout to manage interface components in the form of a form
  • By using QFormLayout, you can see that its settings are relatively simple and clear.
  • QFormLayout, like QBoxLayout and QGridLayout, supports the nesting of layout managers.

This article mainly explains the PyQt5 form layout controls QFormLayout detailed use of methods and examples, more about the use of PyQt5 layout controls, please see the following related links