0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PySide6で実装するUI

0
Last updated at Posted at 2024-08-09

今回はPySide6を用いて,ボタンを配置していきます.HTMLのようなブロック構造を採用しつつ,数値による指定を行わなくても簡単にブロックを配置することができます.

記述例

まず,記述例から見ていきます.解説は後で行います,

from PySide6.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QPushButton, QWidget

app = QApplication([])

window = QWidget()
main_layout = QVBoxLayout()

# サブレイアウトを作成
sub_layout = QHBoxLayout()
button1 = QPushButton('Button 1')
button2 = QPushButton('Button 2')
sub_layout.addWidget(button1)
sub_layout.addWidget(button2)

# サブレイアウトをメインレイアウトに追加
main_layout.addLayout(sub_layout)

# 別のボタンをメインレイアウトに追加
button3 = QPushButton('Button 3')
main_layout.addWidget(button3)

window.setLayout(main_layout)
window.show()

app.exec()

実行結果

図1のように分割されたボタンが出現します.
PySideの表示例2.png
図1 ボタンの表示例

解説

図2のような構成になっています.
PySideの表示例イメージ図.png

図2 エリアの分割図

垂直に分割した後に,一つ目のブロックだけ,水平に分割しています.

QVBoxLayoutとQHBoxLayout

分割方法の決め方には下記の2種類があります.
QVBoxLayoutはvertical(垂直)に要素の数分,分割するクラスです.

box1.png

図3 QVBoxLayout:vertical(垂直)な分割の例

一方,QHBoxLayoutは,horizontal(水平)に要素の数分,分割するクラスです.
例を図4に示します.

box2.png

図4 QHBoxLayout:horizontal(水平)な分割の例

addwidgetとaddlayout

addWidget

addWidget メソッドは、レイアウトにウィジェット(例えば、ボタンやテキストボックスなどのUI要素)を追加するために使用されます。このメソッドを使うと、レイアウト内の特定の位置にウィジェットを配置できます。

addLayout

addLayout メソッドは、レイアウトの中にさらに別のレイアウトを追加するために使用されます。これは、ネストされたレイアウトを構築する際に便利です。たとえば、垂直レイアウトの中に水平レイアウトを配置して、複雑なUIレイアウトを作成する場合などに使用します。

イメージ図としては図5のようになります.

PySideの表示例イメージ図3.png

図5 addwidgetとaddlayoutによる層の種類

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?