PyQt - QGraphicsGridLayout



In PyQt, QGraphicsGridLayout is a layout class used within the Graphics View. It arranges widgets and other layout items in a grid pattern. We can use it to create structured layouts for graphical elements in your GUI-based applications.

Lets explore the advantages of using QGraphicsGridLayout in PyQt −

The class QGraphicsGridLayout provides a grid layout for arranging layout and widgets within a QgraphicsWidget.

Following is an important key point of QGraphicsGridLayout −

  • While creating a structured layout for graphical elements it is always necessary to organize widgets in a grid pattern.
  • Adding widgets and layout by calling method addItem().
  • Assigning layout in widget using the method setLayout().
  • We can construct an object on the heap with no parent.
  • QGraphicsGrid allows each item's size in an integer.
  • If a cell in the grid has extra space that cannot be filled by its items, each item is positioned based on the layout's alignment for that item. To specify an alignment for each item, we can use the setAlignment() method. Similarly, we can check the alignment for any item using the alignment() method.

Now let's understand the advantages of using QGraphicsGridLayout in GUI development −

  • Grid-Based Layouts − The developer can create a sequential layout using QGraphicsGridLayout, especially for positioning widgets in Graphics View.
  • Automated Sizing − The layout automatically calculates item dimensions, simplifying widget placement.
  • Ownership Management − The layout is designed to take on the responsibility of managing added items, which makes memory management and cleanup effortless.
  • Size Control − Respect the size preferences and contraction policies of each item within the grid.
  • Flexible Alignment − Customize the alignment of each item with setAlignment() to achieve the desired appearance.

Note that mastering layouts empowers the professional-looking interfaces.

Example

Following example illustrates the four container widgets using the QGraphicsGridLayout class and its method.

import sys
from PyQt6.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsGridLayout, QGraphicsProxyWidget, QPushButton, QGraphicsWidget
def main():
   app = QApplication(sys.argv)

   # Create a QGraphicsScene
   scene = QGraphicsScene()

   # Create a QGraphicsView and set the scene
   view = QGraphicsView(scene)

   # Create a QGraphicsGridLayout
   layout = QGraphicsGridLayout()

   # Create buttons and add them to the layout
   b1 = QPushButton("Box 1")
   b2 = QPushButton("Box 2")
   b3 = QPushButton("Box 3")
   b4 = QPushButton("Box 4")

   proxy_button1 = QGraphicsProxyWidget()
   proxy_button1.setWidget(b1)
   layout.addItem(proxy_button1, 0, 0)

   proxy_button2 = QGraphicsProxyWidget()
   proxy_button2.setWidget(b2)
   layout.addItem(proxy_button2, 0, 1)

   proxy_button3 = QGraphicsProxyWidget()
   proxy_button3.setWidget(b3)
   layout.addItem(proxy_button3, 1, 0)

   proxy_button4 = QGraphicsProxyWidget()
   proxy_button4.setWidget(b4)
   layout.addItem(proxy_button4, 1, 1)

   # Create a QGraphicsWidget to hold the layout
   widget = QGraphicsWidget()
   widget.setLayout(layout)

   # Add the widget to the scene
   scene.addItem(widget)

   # Show the view
   view.show()

   # Execute the application
   sys.exit(app.exec())

if __name__ == "__main__":
   main()

Output

The above code produces the following output −

qgraphics Grid Layout
Advertisements