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. QList takeLast() and removeLast() conflict
QtWS25 Last Chance

QList takeLast() and removeLast() conflict

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k Views
  • 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.
  • I Offline
    I Offline
    Imhotep
    wrote on last edited by
    #1

    When I try to run this code

    @
    QStringList foo;
    foo << "bar";
    QString last;
    last = foo.takeLast();
    qDebug() << last;
    foo.removeLast();
    @

    Qt Creator raises this error:

    ASSERT: "!isEmpty()" in file ......\Qt\Qt5.3.0\5.3\mingw482_32\include/QtCore/qlist.h, line 299

    If I disable the line with removeLast() command by toggling comment the code works fine.
    I do not understand this behavior since at takeLast() call the QStringList has not yet been emptied.
    Is a bug? is there a way to save the last before QStringList is emptied?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      [quote]I do not understand this behavior since at takeLast() call the QStringList has not yet been emptied.[/quote]It has been emptied. takeLast() removes the last item in your list.

      Your program crashed because you called removeLast() on an empty list.

      The details are in the documentation: http://qt-project.org/doc/qt-5/QList.html (QStringList is a typedef of QList<QString>)

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Imhotep
        wrote on last edited by
        #3

        removeLast() is called before takeLast(). The list is not yet empty at the moment of takeLast() call

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [quote author="Imhotep" date="1405937374"]removeLast() is called before takeLast(). The list is not yet empty at the moment of takeLast() call[/quote]Your original code called takeLast() before removeLast().

          But anyway, if your list contains one item, then...

          • ...calling removeLast() will make your list empty.
          • ...calling takeLast() will make your list empty.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0

          • Login

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