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).


  • Qt Champions 2016

    hi

    QStack <QColor> stackColors; 
      stackColors.push(QColor(0,0,0));
      stackColors.push(QColor(222,222,222));
      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
    

    gives
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.