Skip to content

Commit 1926efa

Browse files
author
Santhosh Kumar
committed
QTabBar: Set style-sheet style as the proxy for the base style
The geometry of the scroll-left (SE_TabBarScrollLeftButton) and scroll- right (SE_TabBarScrollRightButton) buttons can be styled either through the base style or the style-sheet style, but not both, like position set by the base style and width of the button set through style-sheet. This patch fixes this issue by temporarily setting QStyleSheetStyle as the proxy of the base style. This solution has been adapted from the patch: f414c49. Amends 9a3bdbf. Fixes: QTBUG-139588 Pick-to: 6.10 6.8 Change-Id: If86e4ace3efd64f46def0ae338cbb547dcc33412 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
1 parent 439b758 commit 1926efa

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/widgets/styles/qstylesheetstyle.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6301,8 +6301,10 @@ QRect QStyleSheetStyle::subElementRect(SubElement se, const QStyleOption *opt, c
63016301

63026302
case SE_TabBarScrollLeftButton:
63036303
case SE_TabBarScrollRightButton:
6304-
if (hasStyleRule(w, PseudoElement_TabBarScroller))
6305-
return ParentStyle::subElementRect(se, opt, w);
6304+
if (hasStyleRule(w, PseudoElement_TabBarScroller)) {
6305+
QStyleSheetProxySaver proxySaver(this);
6306+
return baseStyle()->subElementRect(se, opt, w);
6307+
}
63066308
break;
63076309

63086310
case SE_TabBarTearIndicator: {

tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <QtWidgets/QToolTip>
2828
#include <QtWidgets/QTreeView>
2929
#include <QtWidgets/QVBoxLayout>
30+
#include <QtWidgets/QProxyStyle>
3031

3132
#include <QtGui/QPainter>
3233
#include <QtGui/QScreen>
@@ -94,6 +95,7 @@ private slots:
9495
void task206238_twice();
9596
void transparent();
9697
void proxyStyle();
98+
void useProxyandStyleSheetStyleInTabBar();
9799
void dialogButtonBox();
98100
void emptyStyleSheet();
99101
void toolTip_data();
@@ -1593,6 +1595,51 @@ void tst_QStyleSheetStyle::proxyStyle()
15931595
delete newProxy;
15941596
}
15951597

1598+
void tst_QStyleSheetStyle::useProxyandStyleSheetStyleInTabBar()
1599+
{
1600+
#ifdef Q_OS_MACOS
1601+
// Since macOS style doesn't support tabbar scroll buttons, skip this case
1602+
QSKIP("TabBar doesn't support scroll button in macOS");
1603+
#endif
1604+
1605+
class CustomProxy : public QProxyStyle
1606+
{
1607+
public:
1608+
QRect subElementRect(SubElement subElement,
1609+
const QStyleOption *option,
1610+
const QWidget *widget) const override {
1611+
auto r = QProxyStyle::subElementRect(subElement, option, widget);
1612+
if (subElement == SE_TabBarScrollLeftButton)
1613+
r.moveLeft(0);
1614+
return r;
1615+
}
1616+
};
1617+
1618+
qApp->setStyleSheet("QTabBar::scroller { width: 50px; }");
1619+
1620+
QTabBar tabBar;
1621+
tabBar.setMaximumWidth(150);
1622+
tabBar.setStyle(new CustomProxy);
1623+
int totalWidth = 0;
1624+
for (int tabIndex = 0; tabIndex < 10; ++tabIndex) {
1625+
tabBar.addTab("Tab " + QString::number(tabIndex));
1626+
totalWidth += tabBar.tabRect(tabIndex).width();
1627+
}
1628+
if (totalWidth < 200)
1629+
tabBar.resize(totalWidth / 2, 30);
1630+
tabBar.show();
1631+
QVERIFY(QTest::qWaitForWindowActive(&tabBar));
1632+
1633+
QToolButton *scrollLeftButton = qobject_cast<QToolButton *>(tabBar.children().at(0));
1634+
QVERIFY(scrollLeftButton);
1635+
QCOMPARE(scrollLeftButton->pos(), QPoint(0, 0));
1636+
QCOMPARE(scrollLeftButton->width(), 25);
1637+
1638+
QToolButton *scrollRightButton = qobject_cast<QToolButton *>(tabBar.children().at(1));
1639+
QVERIFY(scrollRightButton);
1640+
QCOMPARE(scrollRightButton->width(), 25);
1641+
}
1642+
15961643
void tst_QStyleSheetStyle::dialogButtonBox()
15971644
{
15981645
QDialogButtonBox box;

0 commit comments

Comments
 (0)