QGridLayout设置行间距和列间距
时间: 2024-02-06 18:06:18 浏览: 118
要设置QGridLayout的行间距和列间距,可以使用`QGridLayout::setSpacing()`函数。这个函数接受一个整数值,表示行和列之间的间距(单位是像素)。
下面是一个例子,演示如何将一个QLabel和一个QPushButton放置在一个QGridLayout中,并设置行间距和列间距:
```cpp
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
QGridLayout *grid = new QGridLayout(&window);
QLabel *label = new QLabel("Hello");
QPushButton *button = new QPushButton("Click me");
grid->addWidget(label, 0, 0);
grid->addWidget(button, 0, 1);
// 设置行间距和列间距为20像素
grid->setSpacing(20);
window.show();
return app.exec();
}
```
这个例子将行间距和列间距都设置为20像素。如果您想单独设置行间距或列间距,可以使用`QGridLayout::setVerticalSpacing()`和`QGridLayout::setHorizontalSpacing()`函数。