@@ -1479,6 +1479,7 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
1479
1479
p->setRenderHint (QPainter::Antialiasing);
1480
1480
p->setPen (Qt::NoPen);
1481
1481
p->setBrush (Qt::NoBrush);
1482
+ int menuButtonIndicatorMargin = 4 ;
1482
1483
1483
1484
if (toolbutton->state & (State_MouseOver | State_Sunken)) // hover状态 、press状态
1484
1485
p->setBrush (getBrush (toolbutton, DPalette::Button));
@@ -1489,7 +1490,7 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
1489
1490
(toolbutton->state & (QStyle::State_MouseOver | QStyle::State_Sunken))) {
1490
1491
1491
1492
// 绘制外层背景色
1492
- int menuButtonIndicatorMargin = 4 ;
1493
+
1493
1494
auto btn = *toolbutton;
1494
1495
if (btn.state & (QStyle::State_MouseOver))
1495
1496
btn.state &= ~ QStyle::State_MouseOver;
@@ -1532,7 +1533,19 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
1532
1533
p->setPen (getColor (toolbutton, DPalette::ButtonText));
1533
1534
}
1534
1535
1535
- p->drawText (rect, alignment, toolbutton->text );
1536
+ // 只显示文字且为PopupDelay模式且有菜单则文字和箭头整体居中显示(日历中的选择月份和年份的toolButton也遵循这个规则)
1537
+ if ((toolbutton->features & QStyleOptionToolButton::HasMenu && toolbutton->features & QStyleOptionToolButton::PopupDelay && toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly)
1538
+ || w->property (" _d_calendarToolBtn" ).toBool ()) {
1539
+ QFontMetrics metrics (toolbutton->font );
1540
+ int fontWidth = metrics.horizontalAdvance (toolbutton->text );
1541
+ int indicatorWidth = proxy ()->pixelMetric (PM_MenuButtonIndicator, toolbutton, w);
1542
+ int subRectWidth = fontWidth + indicatorWidth + menuButtonIndicatorMargin;
1543
+ QRect subRect = QRect (rect.left () + (rect.width () - subRectWidth) / 2 , rect.top (), subRectWidth, rect.height ());
1544
+ QRect textRect = QRect (subRect.topLeft (), QSize (fontWidth, rect.height ()));
1545
+ p->drawText (textRect, alignment, toolbutton->text );
1546
+ } else {
1547
+ p->drawText (rect, alignment, toolbutton->text );
1548
+ }
1536
1549
} else { // 只显示文字的情景 的 补集
1537
1550
QIcon icon;
1538
1551
QSize pmSize = toolbutton->iconSize ;
@@ -1570,8 +1583,6 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
1570
1583
p->setBrush (getColor (toolbutton, DPalette::Button));
1571
1584
}
1572
1585
1573
-
1574
-
1575
1586
// pr为图标的大小
1576
1587
QRect pr = rect;
1577
1588
// tr为文字的大小
@@ -3316,23 +3327,30 @@ void ChameleonStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleO
3316
3327
newBtn.rect = QRect (ir.right () - mbi - menuButtonIndicatorMargin, (ir.height () - mbi) / 2 , mbi, mbi);
3317
3328
newBtn.rect = visualRect (toolbutton->direction , button, newBtn.rect );
3318
3329
3319
- // DelayedPopup 模式,箭头右居中, 加一个日历 月按钮箭头居中
3320
- if (w && w->objectName () == " qt_calendar_monthbutton" ) {
3321
- newBtn.rect = QRect (ir.right () + 5 - mbi, ir.y () + ir.height () / 2 , mbi - 4 , mbi - 4 );
3322
- newBtn.rect = visualRect (toolbutton->direction , button, newBtn.rect );
3330
+ // 仅文字,DelayedPopup 模式,文字和箭头整体居中
3331
+ if (toolbutton->features & QStyleOptionToolButton::PopupDelay && toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly) {
3332
+ QFontMetrics metrics (toolbutton->font );
3333
+ int fontWidth = metrics.horizontalAdvance (toolbutton->text );
3334
+ int subRectWidth = fontWidth + mbi + menuButtonIndicatorMargin;
3335
+ QRect subRect = QRect (tool.rect .left () + (tool.rect .width () - subRectWidth) / 2 , tool.rect .top (), subRectWidth, tool.rect .height ());
3336
+ QRect indicatorRect = QRect (subRect.topLeft () + QPoint (fontWidth + menuButtonIndicatorMargin, (tool.rect .height () - mbi) / 2 ), QSize (mbi, mbi));
3337
+ newBtn.rect = indicatorRect;
3323
3338
}
3339
+
3324
3340
proxy ()->drawPrimitive (PE_IndicatorArrowDown, &newBtn, p, w);
3325
3341
}
3326
3342
3327
- // 日历 年按钮 特制
3343
+ // 日历 年按钮 特制 文字和箭头整体居中
3328
3344
if (w && w->objectName () == " qt_calendar_yearbutton" ) {
3329
- QStyleOptionToolButton newBtn = *toolbutton;
3330
- int mbi = proxy ()->pixelMetric (PM_MenuButtonIndicator, toolbutton, w);
3331
- QRect ir = toolbutton->rect ;
3332
-
3333
- newBtn.rect = QRect (ir.right () + 5 - mbi, ir.y () + ir.height () / 2 , mbi - 4 , mbi - 4 );
3334
- newBtn.rect = visualRect (toolbutton->direction , button, newBtn.rect );
3335
- proxy ()->drawPrimitive (PE_IndicatorArrowDown, &newBtn, p, w);
3345
+ QStyleOptionToolButton newBtn = *toolbutton;
3346
+ int mbi = proxy ()->pixelMetric (PM_MenuButtonIndicator, toolbutton, w);
3347
+ QFontMetrics metrics (toolbutton->font );
3348
+ int fontWidth = metrics.horizontalAdvance (toolbutton->text );
3349
+ int subRectWidth = fontWidth + mbi + menuButtonIndicatorMargin;
3350
+ QRect subRect = QRect (tool.rect .left () + (tool.rect .width () - subRectWidth) / 2 , tool.rect .top (), subRectWidth, tool.rect .height ());
3351
+ QRect indicatorRect = QRect (subRect.topLeft () + QPoint (fontWidth + menuButtonIndicatorMargin, (tool.rect .height () - mbi) / 2 ), QSize (mbi, mbi));
3352
+ newBtn.rect = indicatorRect;
3353
+ proxy ()->drawPrimitive (PE_IndicatorArrowDown, &newBtn, p, w);
3336
3354
}
3337
3355
}
3338
3356
return ;
0 commit comments