Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use a persistent QGridLayout and add each new entry’s widgets at the same columns on a new row. For example, a selected image can get a filename field in column 0, a width field in column 1, and a height field in column 2. Keep a row counter or a collection of row widgets so you can add, update, or remove entries later.

Set up a grid for repeated entries

A QGridLayout places widgets by row and column, starting at coordinate (0, 0). Give the grid to the containing widget as its layout, or add it to a parent layout if the grid is nested. Once installed, Qt manages child-widget positioning as the parent resizes and the layout contents change. See the Qt 6.12.0 QGridLayout reference and Qt 6.12.0 layout management overview.

This PyQt6 pattern is useful when one logical entry contains several fields. In the image example, each entry has filename, width, and height fields, so each new entry occupies three consistent columns. The PyQt6 Dynamic QR Code Generator tutorial demonstrates adding widgets for selected images; the grid API provides the row-and-column placement used here.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add each entry at the next row

Create the grid once, then create a fresh set of widgets when an entry arrives. Use addWidget(widget, row, column) to place each field, and increment the row index after placing the entry. This example uses a basic window and three fields per image:

from PyQt6.QtWidgets import QApplication, QGridLayout, QLineEdit, QWidget

class ImageFields(QWidget):
    def __init__(self):
        super().__init__()
        self.grid = QGridLayout(self)
        self.next_row = 0
        self.row_widgets = {}

    def add_image(self, filename, width, height):
        row = self.next_row
        fields = [
            QLineEdit(filename),
            QLineEdit(str(width)),
            QLineEdit(str(height)),
        ]

        for column, field in enumerate(fields):
            self.grid.addWidget(field, row, column)

        self.row_widgets[row] = fields
        self.next_row += 1

app = QApplication([])
window = ImageFields()
window.add_image("photo.png", 1280, 720)
window.show()
app.exec()

QGridLayout(self) installs the grid on the containing widget. If your grid is part of a larger interface, create it without a parent widget and add it to the parent layout instead. Keeping the row index and widget references in the application makes the layout coordinates distinct from the underlying entry data.

Make columns grow the way the interface needs

Grid columns are not automatically equal in size. Set column stretch factors when extra width should be divided unevenly; a larger relative factor gives that column more of the available extra space. For example, let a filename field expand more than the width and height fields:

self.grid.setColumnStretch(0, 3)
self.grid.setColumnStretch(1, 1)
self.grid.setColumnStretch(2, 1)

Stretch affects how extra space is distributed; it does not override each widget’s size policy, size hint, or minimum size. Those constraints also influence the final geometry, as described in the QGridLayout reference and layout management overview.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a grid, form, or grouped row widget

  • Use QGridLayout when entries have multiple fields in stable columns, need explicit row-and-column placement, or benefit from cells spanning multiple rows or columns. The grid API supports spanning and per-column stretch.
  • Use QFormLayout for conventional label-and-input rows. Qt describes it as a layout for form input widgets and their associated labels.
  • Group fields in a row widget when each entry needs to be updated, hidden, or removed as one unit. Add each row widget to its own grid row; this can simplify managing the group’s lifecycle.

For the grid and form behaviors, consult the QGridLayout reference and QFormLayout reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Remove or hide an entry later

Qt provides removeWidget(widget) to remove an individual widget from a layout. Hiding a widget also excludes it from layout participation until it is shown again. A grid does not provide a single call that deletes an entire logical row, so use the references you stored for that row to remove or hide each field:

def remove_image(self, row):
    fields = self.row_widgets.pop(row, [])
    for field in fields:
        self.grid.removeWidget(field)
        field.deleteLater()

If later entries must close the gap left by a removed row, rebuild or reposition the remaining widgets using their stored references and data. Alternatively, group each entry’s fields in a row widget and manage that group as a unit. The QGridLayout reference documents widget removal and item-position inspection.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.