October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Add Multi-Field Widget Rows at Runtime in PyQt6 with QGridLayout

Use a persistent QGridLayout and add each new entry’s widgets at a fresh row with consistent column positions. Learn how to size columns and manage removals.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To add several widgets for each new entry in PyQt6, create one QGridLayout for the containing widget, then place each entry’s widgets in the same columns and a new row using addWidget(widget, row, column). For example, an image list can put its filename, width, and height in columns 0, 1, and 2. Keep the layout and row-to-widget references on the object that manages the entries so later updates and removals stay manageable.

Set up a persistent grid

A QGridLayout assigns widgets to cells by row and column. Its top-left cell is (0, 0); the grid grows as items are added. Install it on the widget that contains the entries, or add it to a parent layout if the grid is only one part of a larger interface. Qt’s layout system positions child widgets, responds to resizing, and updates when contents change. See the QGridLayout reference and Qt Layout Management.

As an Amazon Associate I earn from qualifying purchases.

from PyQt6.QtWidgets import QGridLayout, QWidget

class EntryList(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.grid = QGridLayout(self)
        self.next_row = 0
        self.widgets_by_row = {}

Constructing the layout with the containing widget installs it as that widget’s layout. If a grid must sit inside another layout, create it without a parent and insert it into the parent layout instead.

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

Add each entry in stable columns

For every new entry, create its widgets, add them at the current row, and then increment the row index. The column positions stay consistent across entries, so each field type lines up vertically.

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

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

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

This example treats each selected image as one logical entry with filename, width, and height fields, matching the three-column pattern in the PyQt6 layouts tutorial. In an application, use the appropriate widget types for the data and retain references to them if you will read, update, hide, or remove them later.

The row number is an interface coordinate, not the entry’s identity. If entries can be reordered or deleted, keep a mapping from your application’s stable entry ID to its widgets; do not assume a row index remains a permanent identifier.

Control column widths and spanning

Grid columns are not automatically equal in size. Set column stretch factors when extra horizontal space should be divided unevenly. A larger relative factor assigns that column a larger share of the available extra width:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
self.grid.setColumnStretch(0, 3)
self.grid.setColumnStretch(1, 1)
self.grid.setColumnStretch(2, 1)

These factors influence how extra space is distributed; they do not override a widget’s size hint, size policy, or minimum size. If a field needs more than one grid cell, addWidget also accepts row and column spans, for example self.grid.addWidget(widget, row, 0, 1, 3) to span one row across three columns. Check the QGridLayout API for the full method signatures and grid inspection methods, including rowCount(), columnCount(), and itemAtPosition().

Remove or hide an entry’s widgets

Qt provides removeWidget(widget) to remove a widget from layout management. Hiding a widget also excludes it from layout participation until it is shown again. Neither action by itself is a complete row-deletion operation: keep references to the widgets for each logical entry, then act on every widget in that entry.

    def remove_entry(self, row):
        fields = self.widgets_by_row.pop(row, [])
        for widget in fields:
            self.grid.removeWidget(widget)
            widget.deleteLater()

Removing a widget from the layout does not delete the widget, which is why the example schedules each for deletion. If rows may be removed and then new ones appended, a simple increasing row counter can leave gaps; either compact and reposition the remaining entries, or derive coordinates from an ordered collection of active entries.

For interfaces with complex per-entry behavior or frequent row-level updates, consider placing each entry’s fields inside its own custom row widget and adding that single widget to a vertical layout. That groups ownership and cleanup more directly than managing many independent grid items.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose QGridLayout or QFormLayout

Use QGridLayout when each entry has several stable fields, explicit coordinates, cells that span multiple rows or columns, or column-specific stretch behavior. For a conventional label-and-input form, QFormLayout is designed to pair labels with input widgets and may express the structure more clearly. Qt documents both choices in its layout management overview.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.