diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2023-06-20 12:21:20 +0800 |
|---|---|---|
| committer | Mitch Curtis <mitch.curtis@qt.io> | 2023-07-05 13:06:53 +0800 |
| commit | 7d0ec8910f864426ac53aeb12bea505c59f7125a (patch) | |
| tree | 9562b4152e1b38bf7ff8439b408c635b00d80aeb /src/quicktemplates/qquickstackview.cpp | |
| parent | 3e619367b13399eb17224594722af06c6620cf03 (diff) | |
StackView: add strongly-typed pop functions
[ChangeLog][Controls][StackView] Added strongly-typed popToItem
popToIndex, and popCurrentItem functions. These can be compiled to C++
by the QML Compiler.
Task-number: QTBUG-112475
Change-Id: If7452ec238a2d292a64d266ac27bdb146c713017
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/quicktemplates/qquickstackview.cpp')
| -rw-r--r-- | src/quicktemplates/qquickstackview.cpp | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/src/quicktemplates/qquickstackview.cpp b/src/quicktemplates/qquickstackview.cpp index 5202313c4d..2db2e914b6 100644 --- a/src/quicktemplates/qquickstackview.cpp +++ b/src/quicktemplates/qquickstackview.cpp @@ -671,6 +671,10 @@ void QQuickStackView::push(QQmlV4Function *args) stackView.pop(null) \endcode + \note If you are \l {The QML script compiler}{compiling QML}, use the + strongly-typed \l popToItem, \l popToIndex or \l popCurrentItem functions + instead. + \sa clear(), {Popping Items}, {Unwinding Items via Pop} */ void QQuickStackView::pop(QQmlV4Function *args) @@ -709,7 +713,7 @@ void QQuickStackView::pop(QQmlV4Function *args) enter = d->findElement(item); if (!enter) { if (item != d->currentItem) - d->warn(QStringLiteral("unknown argument: ") + value->toQString()); // TODO: safe? + d->warn(QStringLiteral("can't find item to pop: ") + value->toQString()); args->setReturnValue(QV4::Encode::null()); d->elements.push(exit); // restore return; @@ -1109,6 +1113,102 @@ QQuickItem *QQuickStackView::pushItem(const QUrl &url, const QVariantMap &proper } /*! + \qmlmethod Item QtQuick.Controls::StackView::popToItem(item, operation) + \since 6.7 + + Pops all items down to (but not including) \a item. Returns the last item + removed from the stack. + + If \a item is \c null, a warning is produced and \c null is returned. + + \include qquickstackview.qdocinc pop-ownership + + \include qquickstackview.qdocinc operation-values + + If no operation is provided, \c PopTransition will be used. + + \code + stackView.popToItem(someItem, StackView.Immediate) + \endcode + + \sa clear(), {Popping Items}, {Unwinding Items via Pop} +*/ +QQuickItem *QQuickStackView::popToItem(QQuickItem *item, Operation operation) +{ + Q_D(QQuickStackView); + return d->popToItem(item, operation, QQuickStackViewPrivate::CurrentItemPolicy::DoNotPop); +} + +/*! + \qmlmethod Item QtQuick.Controls::StackView::popToIndex(index, operation) + \since 6.7 + + Pops all items down to (but not including) \a index. Returns the last item + removed from the stack. + + If \a index is out of bounds, a warning is produced and \c null is + returned. + + \include qquickstackview.qdocinc pop-ownership + + \include qquickstackview.qdocinc operation-values + + If no operation is provided, \c PopTransition will be used. + + \code + stackView.popToIndex(stackView.depth - 2, StackView.Immediate) + \endcode + + \sa clear(), {Popping Items}, {Unwinding Items via Pop} +*/ +QQuickItem *QQuickStackView::popToIndex(int index, Operation operation) +{ + Q_D(QQuickStackView); + if (index < 0 || index >= d->elements.size()) { + d->warn(QString::fromLatin1("popToIndex: index %1 is out of bounds (%2 item(s))") + .arg(index).arg(d->elements.size())); + return nullptr; + } + + if (index == d->elements.size() - 1) { + // This would pop down to the current item, which is a no-op. + return nullptr; + } + + QQuickStackElement *element = d->elements.at(index); + element->load(this); + return d->popToItem(element->item, operation, QQuickStackViewPrivate::CurrentItemPolicy::Pop); +} + +/*! + \qmlmethod Item QtQuick.Controls::StackView::popCurrentItem(operation) + \since 6.7 + + Pops \l currentItem from the stack. Returns the last item removed from the + stack, or \c null if \l depth was \c 1. + + \include qquickstackview.qdocinc pop-ownership + + \include qquickstackview.qdocinc operation-values + + If no operation is provided, \c PopTransition will be used. + + This function is equivalent to \c popToIndex(stackView.currentIndex - 1). + + \sa clear(), {Popping Items}, {Unwinding Items via Pop} +*/ +QQuickItem *QQuickStackView::popCurrentItem(Operation operation) +{ + Q_D(QQuickStackView); + if (d->elements.size() == 1) { + auto lastItemRemoved = d->elements.last()->item; + clear(operation); + return lastItemRemoved; + } + return d->popToItem(d->currentItem, operation, QQuickStackViewPrivate::CurrentItemPolicy::Pop); +} + +/*! \since QtQuick.Controls 2.3 (Qt 5.10) \qmlproperty bool QtQuick.Controls::StackView::empty \readonly |
