【记录问题】Qt QButtonGroup无效果以及获取流式布局FlowLayout高度
QButtonGroup无效果
QPushButton 默认是不可以被选择,所以需要使用setCheckable(true) 方法设置成可以选择,想要单选效果,需要给QButtonGroup添加setExclusive(true)方法;
获取流式布局FlowLayout高度
/**
* @brief 给出宽度计算出高度
* @param width 宽度
* @return
*/
int FlowLayout::getFlowLayoutHeight(int width) const
{
// 如果FlowLayout中没有子控件,返回0
if (itemList.isEmpty()) {
return 0;
}
int count = 1; // 计算FlowLayout中控件的行数,初始化为1行,不管咋样最少就是一行
int lineHeight = 0; // 当前行中控件的最大高度
int lineWidth = 0; // 当前行已经占据的宽度
int height = 0; // FlowLayout的总高度
QMargins edgeDistance = contentsMargins();//边距
const int itemCount = itemList.count();//获取有多少个子项
width = width - edgeDistance.left() - edgeDistance.right();
for (int i = 0; i < itemCount; i++) {
QLayoutItem *item = itemList.at(i);
//获取到里面的子项
QWidget *widget = item->widget();
QSize s = widget ? widget->sizeHint() : item->sizeHint();
s.setWidth(s.width() + horizontalSpacing()); // 加上水平方向上的间隔
// 如果当前行已经占据的宽度加上当前控件的宽度大于FlowLayout的宽度
if (lineWidth + s.width() > width) {
lineWidth = 0; // 当前行宽度清零
count++; // 行数增加
}
lineWidth += s.width(); // 当前行宽度增加当前控件的宽度
lineHeight = qMax(lineHeight, s.height()); // 更新当前行中控件的最大高度
}
height = lineHeight *count+(verticalSpacing())*(count-1) + edgeDistance.top() +edgeDistance.bottom();
return height + 2; // 返回FlowLayout的总高度
}