Vertical layout QVBoxLayout and horizontal layout QHBoxLayout in Qt

Article directory

QVBoxLayout

Vertical Layout in Qt is a layout manager used to arrange controls in a vertical direction. The following are some commonly used Qt Vertical Layout functions and their usage examples:

  1. Constructor of QVBoxLayout class:
QVBoxLayout *layout = new QVBoxLayout();

Create a vertical layout object using the constructor of the QVBoxLayout class.

  1. The addSpacing() function of the QVBoxLayout class:
layout->addSpacing(10);

Use the addSpacing() function to add a space of a specified size to the layout.

  1. The addWidget() function of the QVBoxLayout class:
QWidget *widget = new QWidget(this);
layout->addWidget(widget);

Use the addWidget() function to add widgets to the layout.

  1. The addLayout() function of the QVBoxLayout class:
QHBoxLayout *hlayout = new QHBoxLayout();
layout->addLayout(hlayout);

Use the addLayout() function to add a sublayout (subvertical layout or horizontal layout) to the main vertical layout.

  1. The setAlignment() function of the QVBoxLayout class:
layout->setAlignment(Qt::AlignCenter);

Use the setAlignment() function to set the alignment of controls in the layout. This is set to center alignment.

  1. The addStretch() function of the QVBoxLayout class:
layout->addStretch(1);

Use the addStretch() function to add a stretchable space to the layout.

  1. insertWidget() function of QVBoxLayout class:
QWidget *widget = new QWidget(this);
layout->insertWidget(0, widget);

Use the insertWidget() function to insert a widget at a specified location.

Summary: The above are some commonly used Qt vertical layout functions and their usage examples. You can choose the corresponding function according to your own needs to achieve the vertical layout effect.


QHBoxLayout

Horizontal Layout in Qt is a layout manager used to arrange controls in the horizontal direction. The following are some commonly used Qt Horizontal Layout functions and their usage examples:

  1. Constructor of QHBoxLayout class:
QHBoxLayout *layout = new QHBoxLayout();

Create a horizontal layout object using the constructor of the QHBoxLayout class.

  1. The addSpacing() function of the QHBoxLayout class:
layout->addSpacing(10);

Use the addSpacing() function to add a space of a specified size to the layout.

  1. The addWidget() function of the QHBoxLayout class:
QWidget *widget = new QWidget(this);
layout->addWidget(widget);

Use the addWidget() function to add widgets to the layout.

  1. addLayout() function of QHBoxLayout class:
QVBoxLayout *vlayout = new QVBoxLayout();
layout->addLayout(vlayout);

Use the addLayout() function to add a sublayout (subvertical layout or horizontal layout) to the main horizontal layout.

  1. The setAlignment() function of the QHBoxLayout class:
layout->setAlignment(Qt::AlignCenter);

Use the setAlignment() function to set the alignment of controls in the layout. This is set to center alignment.

  1. The addStretch() function of the QHBoxLayout class:
layout->addStretch(1);

Use the addStretch() function to add a stretchable space to the layout.

  1. insertWidget() function of QHBoxLayout class:
QWidget *widget = new QWidget(this);
layout->insertWidget(0, widget);

Use the insertWidget() function to insert a widget at a specified location.

Summary: The above are some commonly used Qt horizontal layout functions and their usage examples. You can choose the corresponding function according to your own needs to achieve the horizontal layout effect.


Guess you like

Origin blog.csdn.net/m0_45463480/article/details/132509721