// Create a vertical layout object QVBoxLayout *verticalLayout = new QVBoxLayout(); // Set the layout's object name verticalLayout->setObjectName("myVerticalLayout"); // Add widgets to the layout QLabel *label = new QLabel("Hello, world!"); verticalLayout->addWidget(label);
// Create a parent widget QWidget *parent = new QWidget(); // Set the parent widget's layout as a vertical layout QVBoxLayout *verticalLayout = new QVBoxLayout(parent); // Add widgets to the layout QPushButton *button1 = new QPushButton("Button 1"); verticalLayout->addWidget(button1); QPushButton *button2 = new QPushButton("Button 2"); verticalLayout->addWidget(button2); // Set the layout's object name verticalLayout->setObjectName("myLayout");Here, we set the parent widget's layout as a vertical layout and add two QPushButton widgets to it. We then assign the layout the name "myLayout".