如果你要显示文字,可以使用QLabel组件。以下是一个示例代码,在布局中显示文本和按钮:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout* layout = new QVBoxLayout(&window); // 创建垂直布局
QLabel* label = new QLabel("Hello World!", &window); // 创建标签,显示文本
QPushButton* button = new QPushButton("Button", &window); // 创建按钮
layout->addWidget(label); // 将标签添加到布局中
layout->addWidget(button); // 将按钮添加到布局中
window.setLayout(layout); // 设置窗口的主要布局为上述垂直布局
window.show();
return app.exec();
}
以上代码创建了一个窗口,并在窗口中使用垂直布局(QVBoxLayout)来安排控件。使用QLabel组件显示文本内容,将其添加到垂直布局中。同时,也将QPushButton按钮添加到布局中。
你可以根据需要更改文本内容、调整样式等。希望这个示例能够帮助你开始使用Qt在布局中显示文本和按钮。
内容由零声教学AI助手提供,问题来源于学员提问