From 97d128b1a2027b18e9e788e086a969f60c2e7e6c Mon Sep 17 00:00:00 2001 From: czyt1988 Date: Sun, 17 Dec 2023 00:23:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E9=92=AE=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E5=AE=BD=E5=BA=A6=E4=B8=8D=E4=BC=9A=E5=B0=8F=E4=BA=8E=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E5=AE=BD=E5=BA=A6=EF=BC=8C=E9=81=BF=E5=85=8D=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E7=9A=84=E5=AE=BD=E5=BA=A6=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SARibbonBar/SARibbonToolButton.cpp | 22 +++++++++++--------- src/example/MainWindowExample/mainwindow.cpp | 3 +++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/SARibbonBar/SARibbonToolButton.cpp b/src/SARibbonBar/SARibbonToolButton.cpp index de4a5fb..42dbf95 100644 --- a/src/SARibbonBar/SARibbonToolButton.cpp +++ b/src/SARibbonBar/SARibbonToolButton.cpp @@ -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); @@ -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); @@ -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)); } /** diff --git a/src/example/MainWindowExample/mainwindow.cpp b/src/example/MainWindowExample/mainwindow.cpp index ab07da8..dd61bbe 100644 --- a/src/example/MainWindowExample/mainwindow.cpp +++ b/src/example/MainWindowExample/mainwindow.cpp @@ -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");