From 2140edaaab0bf61f354db521efca773568becc56 Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Thu, 3 Feb 2022 17:05:50 +0100 Subject: [PATCH] Add setCurrentId in QWizard setCurrentId(int id) implemented incl. forward skip warning respective test added in tst_qwizard Fixes: QTBUG-99488 Change-Id: Ieaf31698baf1e8ec97484b4a221c8587385c9fb3 Reviewed-by: Volker Hilsheimer --- src/widgets/dialogs/qwizard.cpp | 36 +++++++++++++++++ src/widgets/dialogs/qwizard.h | 1 + .../widgets/dialogs/qwizard/tst_qwizard.cpp | 40 +++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/src/widgets/dialogs/qwizard.cpp b/src/widgets/dialogs/qwizard.cpp index 5acc7b880f..bf58fdac47 100644 --- a/src/widgets/dialogs/qwizard.cpp +++ b/src/widgets/dialogs/qwizard.cpp @@ -3141,6 +3141,42 @@ void QWizard::next() } } +/*! + Sets currentId to \a id, without visiting the pages between currentId and \a id. + + Returns without page change, if + \list + \li wizard holds no pages + \li current page is invalid + \li given page equals currentId() + \li given page is out of range + \endlist + + Note: If pages have been forward skipped and \a id is 0, page visiting history + will be deleted +*/ + +void QWizard::setCurrentId(int id) +{ + Q_D(QWizard); + + if (d->current == -1) + return; + + if (currentId() == id) + return; + + if (!validateCurrentPage()) + return; + + if (id < 0 || Q_UNLIKELY(!d->pageMap.contains(id))) { + qWarning("QWizard::setCurrentId: No such page: %d", id); + return; + } + + d->switchToPage(id, (id < currentId()) ? QWizardPrivate::Backward : QWizardPrivate::Forward); +} + /*! Restarts the wizard at the start page. This function is called automatically when the wizard is shown. diff --git a/src/widgets/dialogs/qwizard.h b/src/widgets/dialogs/qwizard.h index 8e058aa93b..b1e69b97c0 100644 --- a/src/widgets/dialogs/qwizard.h +++ b/src/widgets/dialogs/qwizard.h @@ -181,6 +181,7 @@ Q_SIGNALS: public Q_SLOTS: void back(); void next(); + void setCurrentId(int id); void restart(); protected: diff --git a/tests/auto/widgets/dialogs/qwizard/tst_qwizard.cpp b/tests/auto/widgets/dialogs/qwizard/tst_qwizard.cpp index 8f655490e1..cc2289572f 100644 --- a/tests/auto/widgets/dialogs/qwizard/tst_qwizard.cpp +++ b/tests/auto/widgets/dialogs/qwizard/tst_qwizard.cpp @@ -89,6 +89,7 @@ private slots: void sideWidget(); void objectNames_data(); void objectNames(); + void changePages(); // task-specific tests below me: void task177716_disableCommitButton(); @@ -2720,6 +2721,45 @@ void tst_QWizard::taskQTBUG_46894_nextButtonShortcut() } } +/* setCurrentId(int) method was added in QTBUG99488 */ +void tst_QWizard::changePages() +{ + QWizard wizard; + + QList pages; + for (int i = 0; i < 4; ++i) { + QWizardPage *page = new QWizardPage; + wizard.addPage(page); + pages.append(page); + } + + wizard.show(); + QVERIFY(QTest::qWaitForWindowExposed(&wizard)); + + // Verify default page + QCOMPARE(wizard.currentPage(), pages.at(0)); + + wizard.next(); + QVERIFY(wizard.currentId() == 1); + wizard.back(); + QVERIFY(wizard.currentId() == 0); + + // Test illegal page + QTest::ignoreMessage(QtMsgType::QtWarningMsg, "QWizard::setCurrentId: No such page: 5"); + wizard.setCurrentId(5); + QCOMPARE(wizard.currentId(), 0); + + for (int i = 0; i < 4; ++i) { + wizard.setCurrentId(i); + QCOMPARE(wizard.currentPage(), pages.at(i)); + } + + for (int i = 3; i >= 0; --i) { + wizard.setCurrentId(i); + QCOMPARE(wizard.currentPage(), pages.at(i)); + } +} + #endif // QT_CONFIG(shortcut) QTEST_MAIN(tst_QWizard)