QTabWidget: Do not add tabbar size during sizeHint() when it is hidden

Since Qt 5.4 the QTabBar can be automatically hidden when it has less
then 2 tabs. Therefore the sizeHint should not consider the tabbars
size when the tabbar is hidden.

Task-number: QTBUG-64715
Change-Id: I2f248f88d9070de5354f7344c7628a78442ab499
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Christian Ehrlicher 2018-02-17 20:46:33 +01:00
parent 3185b40d5d
commit b64f3f6ca9
2 changed files with 53 additions and 12 deletions

View File

@ -195,6 +195,11 @@ public:
void _q_removeTab(int);
void _q_tabMoved(int from, int to);
void init();
bool isAutoHidden() const
{
// see QTabBarPrivate::autoHideTabs()
return (tabs->autoHide() && tabs->count() <= 1);
}
void initBasicStyleOption(QStyleOptionTabWidgetFrame *option) const;
@ -841,11 +846,14 @@ QSize QTabWidget::sizeHint() const
that->setUpLayout(true);
}
QSize s(d->stack->sizeHint());
QSize t(d->tabs->sizeHint());
if(usesScrollButtons())
t = t.boundedTo(QSize(200,200));
else
t = t.boundedTo(QDesktopWidgetPrivate::size());
QSize t;
if (!d->isAutoHidden()) {
t = d->tabs->sizeHint();
if (usesScrollButtons())
t = t.boundedTo(QSize(200,200));
else
t = t.boundedTo(QDesktopWidgetPrivate::size());
}
QSize sz = basicSize(d->pos == North || d->pos == South, lc, rc, s, t);
@ -873,7 +881,9 @@ QSize QTabWidget::minimumSizeHint() const
that->setUpLayout(true);
}
QSize s(d->stack->minimumSizeHint());
QSize t(d->tabs->minimumSizeHint());
QSize t;
if (!d->isAutoHidden())
t = d->tabs->minimumSizeHint();
QSize sz = basicSize(d->pos == North || d->pos == South, lc, rc, s, t);
@ -908,12 +918,14 @@ int QTabWidget::heightForWidth(int width) const
QTabWidget *that = const_cast<QTabWidget*>(this);
that->setUpLayout(true);
}
QSize t(d->tabs->sizeHint());
if(usesScrollButtons())
t = t.boundedTo(QSize(200,200));
else
t = t.boundedTo(QDesktopWidgetPrivate::size());
QSize t;
if (!d->isAutoHidden()) {
t = d->tabs->sizeHint();
if (usesScrollButtons())
t = t.boundedTo(QSize(200,200));
else
t = t.boundedTo(QDesktopWidgetPrivate::size());
}
const bool tabIsHorizontal = (d->pos == North || d->pos == South);
const int contentsWidth = width - padding.width();

View File

@ -98,6 +98,7 @@ private slots:
void heightForWidth();
void tabBarClicked();
void moveCurrentTab();
void autoHide();
private:
int addPage();
@ -713,5 +714,33 @@ void tst_QTabWidget::moveCurrentTab()
QCOMPARE(tabWidget.currentWidget(), secondTab);
}
void tst_QTabWidget::autoHide()
{
QTabWidget tabWidget;
QWidget* firstTab = new QWidget(&tabWidget);
tabWidget.addTab(firstTab, "0");
const auto sizeHint1 = tabWidget.sizeHint();
const auto minSizeHint1 = tabWidget.minimumSizeHint();
const auto heightForWidth1 = tabWidget.heightForWidth(20);
QWidget* secondTab = new QWidget(&tabWidget);
tabWidget.addTab(secondTab, "1");
const auto sizeHint2 = tabWidget.sizeHint();
const auto minSizeHint2 = tabWidget.minimumSizeHint();
const auto heightForWidth2 = tabWidget.heightForWidth(20);
tabWidget.setTabBarAutoHide(true);
// size should not change
QCOMPARE(sizeHint2, tabWidget.sizeHint());
QCOMPARE(minSizeHint2, tabWidget.minimumSizeHint());
QCOMPARE(heightForWidth2, tabWidget.heightForWidth(20));
tabWidget.removeTab(1);
// this size should change now since the tab should be hidden
QVERIFY(sizeHint1.height() > tabWidget.sizeHint().height());
QVERIFY(minSizeHint1.height() > tabWidget.sizeHint().height());
QVERIFY(heightForWidth1 > tabWidget.heightForWidth(20));
}
QTEST_MAIN(tst_QTabWidget)
#include "tst_qtabwidget.moc"