Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QStack, how to remove at specific location.
Qt 6.11 is out! See what's new in the release blog

Using QStack, how to remove at specific location.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 444 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I am using a QStack to create an undo / redo stack:

        class clsQtJsonStack : public QStack<QJsonObject> {
        private:
    
        public:
            clsQtJsonStack();
            int intPush(const QJsonObject& crJSON);
        };
    

    As I edit a control the data is pushed onto the stack. When I undo I take the data from the last undo position, using at. This doesn't remove the item from the stack, how should I do this because looking at the documentation there doesn't appear to be a call to remove an item at a specific index.

    Looking at how to use remove and removeAt, the documentation isn't create because both these expect a qsizetype parameter, is this just an integer cast as qsizetype ?

    Kind Regards,
    Sy

    Christian EhrlicherC 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I am using a QStack to create an undo / redo stack:

          class clsQtJsonStack : public QStack<QJsonObject> {
          private:
      
          public:
              clsQtJsonStack();
              int intPush(const QJsonObject& crJSON);
          };
      

      As I edit a control the data is pushed onto the stack. When I undo I take the data from the last undo position, using at. This doesn't remove the item from the stack, how should I do this because looking at the documentation there doesn't appear to be a call to remove an item at a specific index.

      Looking at how to use remove and removeAt, the documentation isn't create because both these expect a qsizetype parameter, is this just an integer cast as qsizetype ?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @SPlatten said in Using QStack, how to remove at specific location.:

      This doesn't remove the item from the stack, how should I do this because looking at the documentation there doesn't appear to be a call to remove an item at a specific index.

      If it would have those function than it wouldn't be a stack. If you want to remove at a specific index use the proper container.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      SPlattenS 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @SPlatten said in Using QStack, how to remove at specific location.:

        This doesn't remove the item from the stack, how should I do this because looking at the documentation there doesn't appear to be a call to remove an item at a specific index.

        If it would have those function than it wouldn't be a stack. If you want to remove at a specific index use the proper container.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @Christian-Ehrlicher , according to the documentation:
        https://doc.qt.io/qt-6/qstack.html

        The class is derived from QList, QList does have remove:
        https://doc.qt.io/qt-6/qlist.html#remove

        And removeAt:
        https://doc.qt.io/qt-6/qlist.html#removeAt

        Kind Regards,
        Sy

        JonBJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @Christian-Ehrlicher , according to the documentation:
          https://doc.qt.io/qt-6/qstack.html

          The class is derived from QList, QList does have remove:
          https://doc.qt.io/qt-6/qlist.html#remove

          And removeAt:
          https://doc.qt.io/qt-6/qlist.html#removeAt

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @SPlatten
          Then you can use the methods derived from QList. In fact, if you are removing at arbitrary position it is a list rather than a stack anyway, so use that. For a stack you should just use push() and pop().

          As I edit a control the data is pushed onto the stack. When I undo I take the data from the last undo position, using at. This doesn't remove the item from the stack, how should I do this because looking at the documentation there doesn't appear to be a call to remove an item at a specific index.

          This is just T QStack::pop(), and you don't need any *remove at specific index".

          Yes the methods which take a qsizetype as an index accept an integer.

          1 Reply Last reply
          1
          • Axel SpoerlA Offline
            Axel SpoerlA Offline
            Axel Spoerl
            Moderators
            wrote on last edited by
            #5

            @SPlatten: I agree with @Christian-Ehrlicher - it looks scary to remove items from the middle of a stack.
            As you have correctly noticed, QStackis a thin wrapper around QList, which provides access to all its container functions.
            In your research about those, you were just one double click away from the answer to your actual question: qsizetypeis a signed version of size_t.
            using qsizetype = QIntegerForSizeof<std::size_t>::Signed;

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved