C++Qt用QgridLayout实现四个QgroupBox竖着排列时出现四个QgroupBox放置位置没有分开,如何解决
时间: 2023-06-15 09:03:20 浏览: 87
Qt例程源代码QGroupBox.7z
您可以尝试在每个QGroupBox的构造函数中设置一个固定的最小高度,并在QGridLayout中设置每个QGroupBox所在的行高度为该最小高度即可。下面是一个简单的示例代码:
```cpp
#include <QtWidgets>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent)
{
// 创建四个QGroupBox
QGroupBox *group1 = new QGroupBox("Group 1", this);
QGroupBox *group2 = new QGroupBox("Group 2", this);
QGroupBox *group3 = new QGroupBox("Group 3", this);
QGroupBox *group4 = new QGroupBox("Group 4", this);
// 设置每个QGroupBox的最小高度
int minHeight = 80;
group1->setMinimumHeight(minHeight);
group2->setMinimumHeight(minHeight);
group3->setMinimumHeight(minHeight);
group4->setMinimumHeight(minHeight);
// 创建QGridLayout并将四个QGroupBox添加到其中
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(group1, 0, 0);
layout->addWidget(group2, 1, 0);
layout->addWidget(group3, 2, 0);
layout->addWidget(group4, 3, 0);
// 设置每个QGroupBox所在的行高度为最小高度
layout->setRowMinimumHeight(0, minHeight);
layout->setRowMinimumHeight(1, minHeight);
layout->setRowMinimumHeight(2, minHeight);
layout->setRowMinimumHeight(3, minHeight);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
```
上述代码将四个QGroupBox放置在一个QGridLayout中,每个QGroupBox的最小高度为80像素,并且QGridLayout中每个行的高度均为80像素。您可以根据需要更改这些值。
阅读全文