QTabWidget/Bar: ignore hidden tabs for key events

Even a tab was hidden it could be accessed with the key navigation or
a scroll event which lead to painting artifacts.

Pick-to: 6.5 5.15
Fixes: QTBUG-101219
Task-number: QTBUG-63038
Change-Id: I58be694eef5f86cccecbe528891a39a4acdda15f
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit 83e92e25573f98e7530a3dfcaf02910f3932107f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2023-11-06 20:45:41 +01:00 committed by Qt Cherry-pick Bot
parent 35da0bb56e
commit b4384dab06
4 changed files with 49 additions and 2 deletions

View File

@ -2436,7 +2436,7 @@ void QTabBarPrivate::setCurrentNextEnabledIndex(int offset)
{
Q_Q(QTabBar);
for (int index = currentIndex + offset; validIndex(index); index += offset) {
if (tabList.at(index)->enabled) {
if (tabList.at(index)->enabled && tabList.at(index)->visible) {
q->setCurrentIndex(index);
break;
}

View File

@ -1139,7 +1139,7 @@ void QTabWidget::keyPressEvent(QKeyEvent *e)
) {
page = 0;
}
if (d->tabs->isTabEnabled(page)) {
if (d->tabs->isTabEnabled(page) && d->tabs->isTabVisible(page)) {
setCurrentIndex(page);
break;
}

View File

@ -50,6 +50,7 @@ private slots:
void hideTab_data();
void hideTab();
void hideAllTabs();
void checkHiddenTab();
void setElideMode_data();
void setElideMode();
@ -367,6 +368,25 @@ void tst_QTabBar::hideAllTabs()
QVERIFY(sizeHint.width() < prevSizeHint.width());
}
void tst_QTabBar::checkHiddenTab()
{
QTabBar tabbar;
tabbar.addTab("foo");
tabbar.addTab("bar");
tabbar.addTab("baz");
tabbar.setCurrentIndex(0);
tabbar.setTabVisible(1, false);
QKeyEvent keyRight(QKeyEvent::KeyPress, Qt::Key_Right, Qt::NoModifier);
QVERIFY(QApplication::sendEvent(&tabbar, &keyRight));
QCOMPARE(tabbar.currentIndex(), 2);
QKeyEvent keyLeft(QKeyEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
QVERIFY(QApplication::sendEvent(&tabbar, &keyLeft));
QCOMPARE(tabbar.currentIndex(), 0);
}
void tst_QTabBar::setElideMode_data()
{
QTest::addColumn<int>("tabElideMode");

View File

@ -60,6 +60,7 @@ private slots:
void tabPosition();
void tabEnabled();
void tabHidden();
void checkHiddenTab();
void tabText();
void tabShape();
void tabTooltip();
@ -252,6 +253,32 @@ void tst_QTabWidget::tabHidden()
}
}
void tst_QTabWidget::checkHiddenTab()
{
tw->addTab(new QWidget(), "foo");
tw->addTab(new QWidget(), "bar");
tw->addTab(new QWidget(), "baz");
QCOMPARE(tw->count(), 3);
tw->setCurrentIndex(0);
tw->setTabVisible(1, false);
QKeyEvent keyTab(QKeyEvent::KeyPress, Qt::Key_Tab, Qt::ControlModifier);
QVERIFY(QApplication::sendEvent(tw, &keyTab));
QCOMPARE(tw->currentIndex(), 2);
QVERIFY(QApplication::sendEvent(tw, &keyTab));
QCOMPARE(tw->currentIndex(), 0);
QVERIFY(QApplication::sendEvent(tw, &keyTab));
QCOMPARE(tw->currentIndex(), 2);
QKeyEvent keyBacktab(QKeyEvent::KeyPress, Qt::Key_Backtab, Qt::ControlModifier);
QVERIFY(QApplication::sendEvent(tw, &keyBacktab));
QCOMPARE(tw->currentIndex(), 0);
QVERIFY(QApplication::sendEvent(tw, &keyBacktab));
QCOMPARE(tw->currentIndex(), 2);
QVERIFY(QApplication::sendEvent(tw, &keyBacktab));
QCOMPARE(tw->currentIndex(), 0);
}
void tst_QTabWidget::tabText()
{
// Test bad arguments