
- 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 - 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 - QGraphicsAnchorLayout
In GUI building, an Anchor layout is a layout manager that allows user to position and size widget relative to the edge of a container. It is useful when we want to maintain the position of widget regardless of the overall window size. The widget can be anchored to top, left, right, bottom, and center of the container.
In PyQt, a class QGraphicsAnchorLayout provides a layout where one can anchor widgets together in Graphics View.
The anchor layout enables developers to specify the positioning of widgets concerning the layout and each other. Anchors are added to the layout using addAnchor(), addAnchors(), or addCornerAnchors().
Use the anchor() function to access existing anchors in the layout. Anchored items will be automatically added and removed from the layout as needed.
Anchors are always established between the edges of an object, including the "center" as an edge.
Below the given example −
layout.addAnchor(b, Qt.AnchorLeft, a, Qt.AnchorRight) layout.addAnchor(b, Qt.AnchorTop, a, Qt.AnchorBottom)
In the above arrangement, the right edge of item a is attached to the left edge of item b while the bottom edge of item a is attached to the top edge of item b. This results in item b being positioned diagonally to the right and below item a.
Anchor are used to match the widget or height of the width. Thus, this is convenient to use the addAnchor() function.
Example
Following example illustrate the code snippet of QGraphicsAnchorLayout to build the group of containers or boxes.
import sys from PyQt5.QtCore import QSizeF, Qt from PyQt5.QtWidgets import (QApplication, QGraphicsAnchorLayout, QGraphicsProxyWidget, QGraphicsScene, QGraphicsView, QGraphicsWidget, QPushButton, QSizePolicy) def createItem(minimum, preferred, maximum, name): wid = QGraphicsProxyWidget() wid.setWidget(QPushButton(name)) wid.setMinimumSize(minimum) wid.setPreferredSize(preferred) wid.setMaximumSize(maximum) wid.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) return wid if __name__ == '__main__': app = QApplication(sys.argv) scene = QGraphicsScene() scene.setSceneRect(0, 0, 800, 480) minSize = QSizeF(30, 100) prefSize = QSizeF(210, 100) maxSize = QSizeF(300, 100) a = createItem(minSize, prefSize, maxSize, "BOX 1") b = createItem(minSize, prefSize, maxSize, "BOX 2") c = createItem(minSize, prefSize, maxSize, "BOX 3") d = createItem(minSize, prefSize, maxSize, "BOX 4") e = createItem(minSize, prefSize, maxSize, "BOX 5") f = createItem(QSizeF(30, 50), QSizeF(150, 50), maxSize, "BOX 6") g = createItem(QSizeF(30, 50), QSizeF(30, 100), maxSize, "BOX 7") lay = QGraphicsAnchorLayout() lay.setSpacing(0) wid2 = QGraphicsWidget(None, Qt.Window) wid2.setPos(20, 20) wid2.setLayout(lay) # Vertical lay.addAnchor(a, Qt.AnchorTop, lay, Qt.AnchorTop) lay.addAnchor(b, Qt.AnchorTop, lay, Qt.AnchorTop) lay.addAnchor(c, Qt.AnchorTop, a, Qt.AnchorBottom) lay.addAnchor(c, Qt.AnchorTop, b, Qt.AnchorBottom) lay.addAnchor(c, Qt.AnchorBottom, d, Qt.AnchorTop) lay.addAnchor(c, Qt.AnchorBottom, e, Qt.AnchorTop) lay.addAnchor(d, Qt.AnchorBottom, lay, Qt.AnchorBottom) lay.addAnchor(e, Qt.AnchorBottom, lay, Qt.AnchorBottom) lay.addAnchor(c, Qt.AnchorTop, f, Qt.AnchorTop) lay.addAnchor(c, Qt.AnchorVerticalCenter, f, Qt.AnchorBottom) lay.addAnchor(f, Qt.AnchorBottom, g, Qt.AnchorTop) lay.addAnchor(c, Qt.AnchorBottom, g, Qt.AnchorBottom) # Horizontal lay.addAnchor(lay, Qt.AnchorLeft, a, Qt.AnchorLeft) lay.addAnchor(lay, Qt.AnchorLeft, d, Qt.AnchorLeft) lay.addAnchor(a, Qt.AnchorRight, b, Qt.AnchorLeft) lay.addAnchor(a, Qt.AnchorRight, c, Qt.AnchorLeft) lay.addAnchor(c, Qt.AnchorRight, e, Qt.AnchorLeft) lay.addAnchor(b, Qt.AnchorRight, lay, Qt.AnchorRight) lay.addAnchor(e, Qt.AnchorRight, lay, Qt.AnchorRight) lay.addAnchor(d, Qt.AnchorRight, e, Qt.AnchorLeft) lay.addAnchor(lay, Qt.AnchorLeft, f, Qt.AnchorLeft) lay.addAnchor(lay, Qt.AnchorLeft, g, Qt.AnchorLeft) lay.addAnchor(f, Qt.AnchorRight, g, Qt.AnchorRight) scene.addItem(wid2) scene.setBackgroundBrush(Qt.lightGray) view = QGraphicsView(scene) view.show() sys.exit(app.exec_())
Output
The above code produces the following output −
