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
廣告
© . All rights reserved.