PyQt - QGraphicsLinearLayout



在快节奏的软件开发世界中,具有图形用户界面 (GUI) 的桌面应用程序非常重要。这些应用程序允许用户使用交互式且视觉上吸引人的界面与在其计算机上运行的软件进行交互。

QGraphicsLinearLayout 允许您在图形视图中水平或垂直排列小部件。

QGraphicsLinearLayout 是 Qt 控件中的一个布局工具,用于在图形视图框架中组织小部件。它水平(默认)或垂直(通过 setOrientation(Qt.Vertical) 设置)排列小部件。要使用 QGraphicsLinearLayout,请创建一个实例(可选地带有父小部件),并使用 addItem() 添加小部件和布局。布局将拥有已添加的项目。对于继承自 QGraphicsItem 的项目(例如,QGraphicsWidget),请考虑使用 setOwnedByLayout() 来管理所有权。

示例

在下面的示例中,我们使用 QGraphicsLinearLayout 类及其方法创建一个具有黄色背景窗口的简单 PyQt 应用程序。

import sys from PyQt6.QtCore import Qt from PyQt6.QtGui import QPalette, QColor from PyQt6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsLinearLayout, QGraphicsWidget, QMainWindow def create_layout(): # Create a QGraphicsLinearLayout with vertical orientation layout = QGraphicsLinearLayout(Qt.Orientation.Vertical) # Create a QGraphicsWidget to hold the layout container_widget = QGraphicsWidget() # Add the layout to the container widget container_widget.setLayout(layout) return container_widget if __name__ == "__main__": app = QApplication(sys.argv) window = QMainWindow() # Create a QGraphicsView and set the scene view = QGraphicsView() scene = QGraphicsScene() view.setScene(scene) # Add the layout to the scene layout_widget = create_layout() scene.addItem(layout_widget) # Set the background color of the window palette = window.palette() # Set your desired color palette.setColor(QPalette.ColorRole.Window, QColor(255, 255, 0)) window.setPalette(palette) # Adjust the window size window.setGeometry(100, 100, 400, 200) window.show() sys.exit(app.exec())

输出

以上代码产生以下输出:

qgraphics Linear Layout
广告