下面是一个示例代码,展示如何使用Qt创建一个带有两个按钮的垂直布局:
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout *layout = new QVBoxLayout(&window);
QPushButton *button1 = new QPushButton("Button 1");
QPushButton *button2 = new QPushButton("Button 2");
layout->addWidget(button1);
layout->addWidget(button2);
window.setLayout(layout);
window.show();
return app.exec();
}
在上述代码中,我们首先创建了一个QWidget作为容器窗口,并通过指定该窗口为QVBoxLayout的父对象来创建了一个垂直布局。然后,我们创建了两个QPushButton作为子控件,并使用addWidget()方法将它们添加到垂直布局中。最后,通过setLayout()方法将垂直布局设置给窗口,并显示出来。
运行这段代码,你将会看到一个带有两个按钮的窗口,按钮会按照从上到下的顺序排列。你可以根据需要进行更多的布局和控件调整。
内容由零声教学AI助手提供,问题来源于学员提问