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. clear QList method
Qt 6.11 is out! See what's new in the release blog

clear QList method

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

    i have a QList member of ClassA, what should i do if i want to clear this QList?
    (1)m_listA.clear()

    (2)QList<A*>().swap(m_listA)

    (3)for (QList<A>::iterator it = m_listA.begin(); it != m_listA.end(); it++)
    {
    A* a= it;
    if (a)
    {
    delete a;
    a = NULL;
    }
    }
    //clear
    if (!m_listA.isEmpty())
    QList<A
    >().swap(m_listA);

    which is the best and efficient way to do this , thank you!

    1 Reply Last reply
    0
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by
      #2

      Hi,

      You can use QList::clear() if you want to remove all the items from your list.
      If you want to remove only few specific contents you can use QList::remveAll(const T &value).
      And also there is a function qDeleteAll from which you can delete the contents of list

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      2
      • O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        how about the pointer?
        i use QList<A*>
        should i write like this:
        for (QList<A*>::iterator it = m_listA.begin(); it != m_listA.end(); it++)
        {
        A* a= *it;
        if (a)
        {
        delete a;
        a = NULL;
        }
        }
        //clear
        if (!m_listA.isEmpty())
        m_listA.clear();

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          qDeleteAll is perfectly suited for that

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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