QBoxLayout Class

QBoxLayoutクラスは、子ウィジェットを水平または垂直に並べます。詳細...

ヘッダー #include <QBoxLayout>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
を継承する: QLayout
によって継承される:

QHBoxLayout そしてQVBoxLayout

パブリックな型

enum Direction { LeftToRight, RightToLeft, TopToBottom, BottomToTop }

パブリック関数

QBoxLayout(QBoxLayout::Direction dir, QWidget *parent = nullptr)
virtual ~QBoxLayout()
void addLayout(QLayout *layout, int stretch = 0)
void addSpacerItem(QSpacerItem *spacerItem)
void addSpacing(int size)
void addStretch(int stretch = 0)
void addStrut(int size)
void addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
QBoxLayout::Direction direction() const
void insertItem(int index, QLayoutItem *item)
void insertLayout(int index, QLayout *layout, int stretch = 0)
void insertSpacerItem(int index, QSpacerItem *spacerItem)
void insertSpacing(int index, int size)
void insertStretch(int index, int stretch = 0)
void insertWidget(int index, QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())
void setDirection(QBoxLayout::Direction direction)
void setStretch(int index, int stretch)
bool setStretchFactor(QWidget *widget, int stretch)
bool setStretchFactor(QLayout *layout, int stretch)
int stretch(int index) const

再実装パブリック関数

virtual void addItem(QLayoutItem *item) override
virtual int count() const override
virtual Qt::Orientations expandingDirections() const override
virtual bool hasHeightForWidth() const override
virtual int heightForWidth(int w) const override
virtual void invalidate() override
virtual QLayoutItem *itemAt(int index) const override
virtual QSize maximumSize() const override
virtual int minimumHeightForWidth(int w) const override
virtual QSize minimumSize() const override
virtual void setGeometry(const QRect &r) override
virtual void setSpacing(int spacing) override
virtual QSize sizeHint() const override
virtual int spacing() const override
virtual QLayoutItem *takeAt(int index) override

詳細説明

QBoxLayout は、(親レイアウトまたはparentWidget() から)取得したスペースをボックスの列に分割し、各管理対象ウィジェットを 1 つのボックスいっぱいに配置します。

Horizontal box layout with five child widgets

QBoxLayout の向きがQt::Horizontal の場合、ボックスは適切なサイズで一列に配置されます。各ウィジェット(または他のボックス)は、少なくともその最小サイズ、最大サイズを取得します。余分なスペースはストレッチ係数に従って共有されます(詳細は後述します)。

Vertical box layout with five child widgets

QBoxLayout の Orientation がQt::Vertical の場合、ボックスは適切なサイズで列に配置されます。

QBoxLayout を作成する最も簡単な方法は、QHBoxLayoutQt::Horizontal ボックス用)やQVBoxLayoutQt::Vertical ボックス用)など、便利なクラスのいずれかを使用することです。QBoxLayout コンストラクタを直接使用して、その方向をLeftToRightRightToLeftTopToBottomBottomToTop のように指定することもできます。

QBoxLayout がトップ・レベルのレイアウトでない場合(つまり、ウィジェットのすべての領域と子を管理していない場合)、QBoxLayout で何かを行う前に、そのレイアウトを親レイアウトに追加する必要があります。レイアウトを追加する通常の方法は、parentLayout->addLayout()を呼び出すことです。

これを行うと、4つの関数のいずれかを使用してQBoxLayoutにボックスを追加できます:

  • addWidget() を使用してQBoxLayoutにウィジェットを追加し、ウィジェットのストレッチ・ファクターを設定します。() を使用して、QBoxLayout にボックスを追加します。
  • addSpacing() で空のボックスを作成します。これは、美しく広々としたダイアログを作成するために使用する関数の1つです。マージンを設定する方法については以下を参照してください。
  • addStretch() で、伸縮可能な空のボックスを作成します。
  • addLayout() で、別のQLayout を含むボックスを行に追加し、そのレイアウトの伸縮率を設定します。

insertWidget()、insertSpacing()、insertStretch()、insertLayout() を使用して、レイアウトの指定位置にボックスを挿入します。

QBoxLayoutには、2つのマージン幅もあります:

  • setContentsMargins() は、ウィジェットの各辺の外枠の幅を設定します。これは、QBoxLayout の 4 つの側面に沿って確保されたスペースの幅です。
  • setSpacing() は、隣り合うボックス間の幅を設定します。(addSpacing()を使用して、特定の場所でより多くのスペースを得ることができます)。

marginのデフォルトはスタイルによって指定されます。ほとんどの Qt スタイルが指定するデフォルトのマージンは、子ウィジェットでは 9、ウィンドウでは 11 です。スペーシングのデフォルトは、トップレベルレイアウトのマージン幅と同じか、親レイアウトと同じです。

レイアウトからウィジェットを削除するには、removeWidget ()を呼び出します。ウィジェットでQWidget::hide ()を呼び出すと、QWidget::show ()が呼び出されるまで、そのウィジェットはレイアウトから効果的に削除されます。

便利なコンストラクタがあるため、ほとんどの場合、QBoxLayout ではなくQVBoxLayoutQHBoxLayout を使用します。

QGridLayoutQStackedLayout 、およびLayout Managementも参照してください

メンバ・タイプのドキュメント

enum QBoxLayout::Direction

この型は、ボックスレイアウトの方向を決定するために使用される。

定数説明
QBoxLayout::LeftToRight0左から右への水平
QBoxLayout::RightToLeft1右から左への水平
QBoxLayout::TopToBottom2上から下への垂直
QBoxLayout::BottomToTop3下から上への垂直

メンバー関数ドキュメント

[explicit] QBoxLayout::QBoxLayout(QBoxLayout::Direction dir, QWidget *parent = nullptr)

方向dir と親ウィジェットparent を持つ新しい QBoxLayout を構築します。

レイアウトは、parent のトップ・レベル・レイアウトとして直接設定されます。ウィジェットのトップレベル レイアウトは 1 つだけです。それはQWidget::layout() によって返されます。

direction() およびQWidget::setLayout()も参照してください

[virtual noexcept] QBoxLayout::~QBoxLayout()

このボックス・レイアウトを破壊します。

レイアウトのウィジェットは破壊されません。

[override virtual] void QBoxLayout::addItem(QLayoutItem *item)

再実装:QLayout::addItem(QLayoutItem *item)。

void QBoxLayout::addLayout(QLayout *layout, int stretch = 0)

ボックスの末尾にlayout を、直列ストレッチ係数stretch で追加する。

insertLayout(),addItem(),addWidget()も参照のこと

void QBoxLayout::addSpacerItem(QSpacerItem *spacerItem)

このボックスレイアウトの最後にspacerItem を追加する。

addSpacing() およびaddStretch()も参照

void QBoxLayout::addSpacing(int size)

こ の枠レ イ ア ウ ト の末尾に、 サ イ ズsize の伸縮不可スペース (QSpacerItem ) を追加 し ます。QBoxLayout にはデフ ォル ト の余白 と 間隔があ り ます。こ の関数は余白を追加 し ます。

insertSpacing ()、addItem ()、QSpacerItemも参照

void QBoxLayout::addStretch(int stretch = 0)

こ の枠レ イ ア ウ ト の末尾に、 最小サ イ ズが 0 で伸縮倍率がstretch の伸縮可能な スペース (QSpacerItem ) を追加 し ます。

insertStretch(),addItem(),QSpacerItemも参照

void QBoxLayout::addStrut(int size)

枠の垂直方向の寸法 (た と えば、 枠がLeftToRight の と き の高 さ ) を最小値size に制限 し ます。 その他の制約を指定する と 、 こ の制限値が増加す る 可能性があ り ます。

addItem()も参照してください

void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

このボックスレイアウトの末尾にwidget を追加します。ストレッチファクターはstretch で、アライメントはalignment です。

ストレッチ・ファクターはQBoxLayoutdirection 内にのみ適用され、このQBoxLayout 内の他のボックスやウィジェットに対する相対的なものです。ストレッチ・ファクターが高いウィジェットやボックスほど大きくなります。

ストレッチ・ファクターが 0 で、QBoxLayout 内に 0 より大きいストレッチ・ファクターを持つものがない場合、関係する各ウィジェットのQWidget:sizePolicy() に従ってスペースが分配されます。

アライメントはalignment で指定します。デフォルトのアライメントは 0 で、これはウィジェットがセル全体を埋めることを意味します。

insertWidget(),addItem(),addLayout(),addStretch(),addSpacing(),addStrut()も参照

[override virtual] int QBoxLayout::count() const

再実装:QLayout::count() const.

QBoxLayout::Direction QBoxLayout::direction() const

ボックスの方向を返します。addWidget() およびaddSpacing() はこの方向で動作し、stretch はこの方向に伸びる。

setDirection(),QBoxLayout::Direction,addWidget(),addSpacing()も参照

[override virtual] Qt::Orientations QBoxLayout::expandingDirections() const

再実装:QLayout::expandingDirections() const.

[override virtual] bool QBoxLayout::hasHeightForWidth() const

再実装:QLayoutItem::hasHeightForWidth() const.

[override virtual] int QBoxLayout::heightForWidth(int w) const

再インプリメント:QLayoutItem::heightForWidth(int) const.

void QBoxLayout::insertItem(int index, QLayoutItem *item)

index の位置にitem を挿入する。インデックスは負か、0 からcount() までの範囲内でなければならない。index が負またはcount() の場合、項目は最後に追加される。

addItem()、insertWidget()、insertLayout()、insertStretch()、insertSpacing()も参照のこと

void QBoxLayout::insertLayout(int index, QLayout *layout, int stretch = 0)

index の位置にlayout を挿入し、伸張倍率はstretch とする。index が負の場合、レイアウトは最後に追加されます。

layout ボックスレイアウトの子になります。

addLayout() およびinsertItem()も参照

void QBoxLayout::insertSpacerItem(int index, QSpacerItem *spacerItem)

index の位置にspacerItem を挿入し、最小サイズと伸張率をゼロにする。index が負の場合、スペースは最後に追加される。

addSpacerItem()、insertStretch()、insertSpacing()も参照

void QBoxLayout::insertSpacing(int index, int size)

位置index に、サイズsize の伸縮不可能な空白 (aQSpacerItem) を挿入する。index が負の場合、 空白は末尾に追加 さ れます。

ボ ッ ク ス レ イ ア ウ ト にはデフ ォル ト の余白 と 間隔があ り ます。こ の関数は余白を追加 し ます。

addSpacing ()、insertItem ()、QSpacerItemも参照

void QBoxLayout::insertStretch(int index, int stretch = 0)

伸縮可能な空白 (aQSpacerItem ) を位置index に挿入 し 、 その最小サ イ ズ と 伸縮倍率はゼロstretchindex が負の場合、スペースは最後に追加されます。

addStretch()、insertItem()、QSpacerItemも参照

void QBoxLayout::insertWidget(int index, QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment())

index の位置にwidget を挿入し、ストレッチファクターstretch とアライメントalignment を指定する。index が負の場合、ウィジェットは最後に追加されます。

ストレッチ・ファクターは、QBoxLayoutdirection にのみ適用され、このQBoxLayout 内の他のボックスやウィジェットに対する相対的なものです。ストレッチ・ファクターが高いウィジェットやボックスは、より大きく成長します。

ストレッチ・ファクターが 0 で、QBoxLayout 内に 0 より大きいストレッチ・ファクターを持つものがない場合、関係する各ウィジェットのQWidget:sizePolicy() に従ってスペースが分配されます。

アライメントはalignment で指定します。デフォルトのアライメントは 0 で、これはウィジェットがセル全体を埋めることを意味します。

addWidget() およびinsertItem()も参照してください

[override virtual] void QBoxLayout::invalidate()

再インプリメント:QLayout::invalidate().

キャッシュされた情報をリセットします。

[override virtual] QLayoutItem *QBoxLayout::itemAt(int index) const

再実装:QLayout::itemAt(int index) const.

[override virtual] QSize QBoxLayout::maximumSize() const

再実装:QLayout::maximumSize() const.

[override virtual] int QBoxLayout::minimumHeightForWidth(int w) const

再インプリメント:QLayoutItem::minimumHeightForWidth(int w) const.

[override virtual] QSize QBoxLayout::minimumSize() const

再実装:QLayout::minimumSize() const.

void QBoxLayout::setDirection(QBoxLayout::Direction direction)

このレイアウトの方向をdirection に設定する。

direction()も参照

[override virtual] void QBoxLayout::setGeometry(const QRect &r)

再実装:QLayout::setGeometry(const QRect &r).

[override virtual] void QBoxLayout::setSpacing(int spacing)

QLayout::spacing

QLayout::setSpacing() を再実装。spacing プロパティをspacing に設定します。

QLayout::setSpacing() およびspacing()も参照してください

void QBoxLayout::setStretch(int index, int stretch)

位置index.のストレッチファクターをstretch.に設定する。

stretch()も参照

bool QBoxLayout::setStretchFactor(QWidget *widget, int stretch)

widget false の伸縮率を に設定 し 、 が こ の レ イ ア ウ ト (子レ イ ア ウ ト を含まず) 内に見つかれば true を返 し ます。stretch widget

setAlignment()も参照してください

bool QBoxLayout::setStretchFactor(QLayout *layout, int stretch)

これはオーバーロードされた関数です。

レイアウトlayout のストレッチファクターをstretch に設定し、layout がこのレイアウト(子レイアウトを含まない)で見つかった場合はtrue を返し、そうでない場合はfalse を返します。

[override virtual] QSize QBoxLayout::sizeHint() const

再実装:QLayoutItem::sizeHint() const.

[override virtual] int QBoxLayout::spacing() const

QLayout::spacing

QLayout::spacing() を再実装。spacing プロパティが有効な場合は、その値が返されます。そうでない場合は、spacingプロパティの値が計算されて返されます。ウィジェットのレイアウト間隔はスタイルに依存するため、親がウィジェットの場合、レイアウトの(水平または垂直)間隔をスタイルに問い合わせます。そうでない場合、親はレイアウトであり、親レイアウトに spacing() を問い合わせます。

QLayout::spacing() およびsetSpacing()も参照してください

int QBoxLayout::stretch(int index) const

位置index におけるストレッチ係数を返す。

setStretch()も参照

[override virtual] QLayoutItem *QBoxLayout::takeAt(int index)

再インプリメント:QLayout::takeAt(int index).

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.