Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-85903

StackView: calling pop() with one item does nothing

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • P3: Somewhat important
    • None
    • 5.15
    • Quick: Controls 2
    • None

    Description

      https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#pop-method says:

      Pops one or more items off the stack. Returns the last item removed from the stack.

      If the item argument is specified, all items down to (but not including) item will be popped. If item is null, all items down to (but not including) the first item is popped. If not specified, only the current item is popped.

      However, if you call pop() when a StackView only has one item, nothing will happen. That seems to be because of this line. This patch I quickly threw together seems to fix it:

      diff --git a/src/quicktemplates2/qquickstackview.cpp b/src/quicktemplates2/qquickstackview.cpp
      index b281d79d..00f6f75c 100644
      --- a/src/quicktemplates2/qquickstackview.cpp
      +++ b/src/quicktemplates2/qquickstackview.cpp
      @@ -667,17 +667,20 @@ void QQuickStackView::pop(QQmlV4Function *args)
       
           QScopedValueRollback<bool> modifyingElements(d->modifyingElements, true);
           QScopedValueRollback<QString> operationNameRollback(d->operation, operationName);
      +    if (d->elements.isEmpty()) {
      +        args->setReturnValue(QV4::Encode::null());
      +        return;
      +    }
           int argc = args->length();
      -    if (d->elements.count() <= 1 || argc > 2) {
      -        if (argc > 2)
      -            d->warn(QStringLiteral("too many arguments"));
      +    if (argc > 2) {
      +        d->warn(QStringLiteral("too many arguments"));
               args->setReturnValue(QV4::Encode::null());
               return;
           }
       
           int oldDepth = d->elements.count();
           QQuickStackElement *exit = d->elements.pop();
      -    QQuickStackElement *enter = d->elements.top();
      +    QQuickStackElement *enter = !d->elements.isEmpty() ? d->elements.top() : nullptr;
       
           QV4::ExecutionEngine *v4 = args->v4engine();
           QV4::Scope scope(v4);
      diff --git a/src/quicktemplates2/qquickstackview_p.cpp b/src/quicktemplates2/qquickstackview_p.cpp
      index e9dc5f35..728a60d4 100644
      --- a/src/quicktemplates2/qquickstackview_p.cpp
      +++ b/src/quicktemplates2/qquickstackview_p.cpp
      @@ -196,6 +196,9 @@ bool QQuickStackViewPrivate::pushElement(QQuickStackElement *element)
       bool QQuickStackViewPrivate::popElements(QQuickStackElement *element)
       {
           Q_Q(QQuickStackView);
      +    if (elements.isEmpty())
      +        return true;
      +
           while (elements.count() > 1 && elements.top() != element) {
               delete elements.pop();
               if (!element)
      

      Attachments

        For Gerrit Dashboard: QTBUG-85903
        # Subject Branch Project Status CR V

        Activity

          People

            mitch_curtis Mitch Curtis
            mitch_curtis Mitch Curtis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes