Skip to content

Commit

Permalink
按钮的最小宽度不会小于图标宽度,避免按钮的宽度异常
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt1988 committed Dec 16, 2023
1 parent 14a6763 commit 97d128b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/SARibbonBar/SARibbonToolButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void SARibbonToolButton::PrivateData::calcSmallButtonDrawRects(const QStyleOptio
case Qt::ToolButtonIconOnly: {
if (hasIndicator(opt)) {
// 在仅有图标的小模式显示时,预留一个下拉箭头位置
iconRect = opt.rect.adjusted(spacing, spacing, -indicatorLen - spacing, -spacing);
iconRect = opt.rect.adjusted(spacing, spacing, -indicatorLen - spacing, -spacing);
indicatorArrowRect = QRect(opt.rect.right() - indicatorLen - spacing, iconRect.y(), indicatorLen, iconRect.height());
} else {
iconRect = opt.rect.adjusted(spacing, spacing, -spacing, -spacing);
Expand All @@ -281,7 +281,7 @@ void SARibbonToolButton::PrivateData::calcSmallButtonDrawRects(const QStyleOptio
case Qt::ToolButtonTextOnly: {
if (hasIndicator(opt)) {
// 在仅有图标的小模式显示时,预留一个下拉箭头位置
textRect = opt.rect.adjusted(spacing, spacing, -indicatorLen - spacing, -spacing);
textRect = opt.rect.adjusted(spacing, spacing, -indicatorLen - spacing, -spacing);
indicatorArrowRect = QRect(opt.rect.right() - indicatorLen - spacing, spacing, indicatorLen, textRect.height());
} else {
textRect = opt.rect.adjusted(spacing, spacing, -spacing, -spacing);
Expand Down Expand Up @@ -484,26 +484,28 @@ QSize SARibbonToolButton::PrivateData::calcLargeButtonSizeHint(const QStyleOptio
int w = 0;
int h = opt.fontMetrics.lineSpacing() * 4.5; // 3*1.5
int minW = h * 0.75; // 最小宽度,在pannel里面的按钮,最小宽度要和icon适应
if (mDrawIconRect.isValid()) {
minW = mDrawIconRect.width();
}

if (SARibbonPannel* pannel = qobject_cast< SARibbonPannel* >(q_ptr->parent())) {
// 对于建立在SARibbonPannel的基础上的大按钮,把高度设置为SARibbonPannel计算的大按钮高度
h = pannel->largeHeight();
}
int textHeight = calcTextDrawRectHeight(opt);
if (mDrawIconRect.isValid()) {
minW = qMax(mDrawIconRect.width(), h - textHeight);
}
// 估算字体的宽度作为宽度
w = estimateLargeButtonTextWidth(h, calcTextDrawRectHeight(opt), opt.text, opt.fontMetrics);
w = estimateLargeButtonTextWidth(h, textHeight, opt.text, opt.fontMetrics);
w += (2 * mSpacing);
// 判断是否需要加上indicator
if (isEnableWordWrap() && isTextNeedWrap()) {
w += mIndicatorLen;
}

if (w < minW) {
w = minW;
}
// if (w < minW) {
// w = minW;
// }
//! Qt6.4 取消了QApplication::globalStrut
return QSize(w, h).expandedTo(QSize(2, 2));
return QSize(w, h).expandedTo(QSize(minW, textHeight));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/example/MainWindowExample/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,9 @@ void MainWindow::createContextCategoryPage1(SARibbonCategory* page)
mActionDisable->setEnabled(true);
mActionDisable->setText(("Enabled"));
});
auto act = createAction(tr("1"), ":/icon/icon/unlock.svg");
act->setToolTip(tr("very short string"));
pannel->addLargeAction(act);

mActionSetTextTest = createAction("set text", ":/icon/icon/setText.svg");

Expand Down

0 comments on commit 97d128b

Please sign in to comment.