
- 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 - QGraphicsLayout
The QGraphicsLayout represents the class of layout that acts as a base class for all graphics views. This view can be used to create a large number of two-dimensional custom items. Typically, we can say it is an abstract class of Qt framework that uses virtual classes and method for arranging children within QGraphicsWidgets. The class QGraphicsWidgets is considered as the base class of all widget classes.
In context of QGraphicsLayout, each layout type uses its own specific class and method such as grid layout, anchor layout, and linear layout.
The QGraphicsLayout inherits from QGraphicsLayoutItem. As a result, this layout achieved through its own subclasses.
Custom Layout QGraphicsLayout
We can use QGraphicsLayout as a base class for creation of custom layout. However, this class uses its subclasses like QGraphicsLinearLayout or QGraphicsGridLayout.
Building a custom layout using various essential functions −
- setGeometry() − This function notify when layout's geometry set.
- sizeHint() − It provide the size hints for the layout.
- count() − It returns the number of items present in the layout.
- itemAt() − This function retrieve the items in the layout.
- removeAt() − This function remove the item without destroying it.
Note that, we can write our own custom layout based on layout requirement and using classes with its method.
Example
Following example illustrate two rectangle in different position with its color using various classes and method of PyQt.
import sys from PyQt6.QtCore import Qt from PyQt6.QtGui import QColor from PyQt6.QtWidgets import QApplication, QGraphicsRectItem, QGraphicsScene, QGraphicsView class CustomGraphicsItem(QGraphicsRectItem): def __init__(self, width, height, color): super().__init__() self.setRect(0, 0, width, height) self.setBrush(QColor(color)) if __name__ == "__main__": app = QApplication(sys.argv) scene = QGraphicsScene() view = QGraphicsView(scene) # Create custom graphics items box1 = CustomGraphicsItem(100, 50, "lightblue") box2 = CustomGraphicsItem(80, 30, "lightgreen") # Position the items box1.setPos(10, 10) box2.setPos(10, 70) # Add items to the scene scene.addItem(box1) scene.addItem(box2) view.show() sys.exit(app.exec())
Output
The above code produces the following output −
