setLayout()函数是Qt中的一个函数,用于设置QWidget中的布局管理器。它接受一个QLayout对象作为参数,并将该布局管理器应用于QWidget。
例如,在以下代码中,setLayout()函数被用于将QHBoxLayout对象(horizontalBoxLayout)应用于QWidget窗口(window):
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QHBoxLayout *horizontalBoxLayout = new QHBoxLayout(&window);
QLabel *label1 = new QLabel("Label 1");
QLabel *label2 = new QLabel("Label 2");
horizontalBoxLayout->addWidget(label1);
horizontalBoxLayout->addWidget(label2);
window.setLayout(horizontalBoxLayout);
window.show();
return app.exec();
}
上述代码创建了一个QWidget窗口,然后创建了一个QHBoxLayout对象,并向其添加两个QLabel控件。最后,setLayout()函数被用于将该布局管理器应用于QWidget窗口。这使得QWidget窗口以水平方向显示这两个QLabel控件。