Qt6 topics merged with General

QStack pop() does not remove the item from the stack

  • Hi,

                    QStack <QColor> stackColors; // QStack declaration ... push 2 values onto the stack
                    qDebug()<<" getColor count a "<<cell->getStackColors().count(); // out -> 2 
                    cell->getStackColors().removeLast();  // should reduce the count to 1 
                    qDebug()<<" getColor count b "<<cell->getStackColors().count();  // again out -> 2 

    So apparently the removeLast() does not reduce the number of items on the stack. I tried pop() and nothing. Have I misunderstood the concept since the documents explain that the removeLast "Removes the last item in the vector" , (stack).

  • Lifetime Qt Champion


    QStack <QColor> stackColors; 
      qDebug() << " getColor count a " << stackColors.count(); // out -> 2
      stackColors.removeLast();  // should reduce the count to 1
      qDebug() << " getColor count b " << stackColors.count(); // again out -> 2

    getColor count a 2
    getColor count b 1

    so my guess is that cell->getStackColors() returns a COPY of your list.
    If you are not using & in return type, this might be why.
    (it creates a copy , not returning the original list)

  • @mrjj ,

    You people are incredibly good.

    Thank you so much.

Log in to reply