
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
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 −
