Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:43:14+00:00 2026-06-09T11:43:14+00:00

I have a QList variable. How can I erase all the elements starting from

  • 0

I have a QList variable. How can I erase all the elements starting from an X index and removing all the subsequents N elements taking care of freeing also the memory?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:43:16+00:00Added an answer on June 9, 2026 at 11:43 am

    First of all, you should never use MyClass* to “own” (i.e. control the lifetime of) an instance of MyClass. Thus, a list of MyClass* shouldn’t “own” any instances of MyClass, either. Thus you wouldn’t ever use the delete operator with any of these pointers (and a stray delete outside a smartpointer implementation should almost always raise a few eyebrows).

    QList is in fact a type optimized for a similar use-case. A QList<MyClass> will internally store a dynamic array of MyClass* (if MyClass is larger than a pointer), so re-ordering and expanding the list is potentially cheaper. However, if you need to remove elements from the list without deleting them, you should use a QList<unique_ptr<MyClass>> or similar. Either way, you wouldn’t have to worry about the objects’ lifetimes; you’d only need to remove them from the list.

    Now that I said that, there are a few useful STL idioms that will help achieving what you’re looking for, and they also apply to (most) Qt containers (edit: fixed the removal, added a few helpful comments):

    QList<MyClass*> myList; //TODO FIXME XXX: clarify mentioned ownership issues
    assert(X + N <= myList.size());
    
    // this is the range of objects we want to remove and delete
    // (N elements, starting from index X)
    auto range_begin = myList.begin() + X,
         range_end = range_begin + N;
    // delete each object in range
    std::for_each(range_begin, range_end, [](MyClass* ptr) { delete ptr; });
    // remove them from the list
    myList.erase(range_begin, range_end);
    

    It’s even simpler still, if you meant to remove all elements after myList[X - 1] (in short, replace range_end with myList.end()).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question about removing element from QList. myclass.h: class node2D : public QObject
I have a QList list. I want to insert it on the database. I
Situation I have a QList<QVariant> where each QVariant item should be a QList<QVariant> of
I have a series of classes representing smart map elements: MapTextElement , MapIconElement ,
Greetings all, I have a code snippet as follows : class AppCtx { private:
I have a QList which I have inserted pointers of objects into. I am
I have this application that requires me to have a QList which will contain
My problem is this. I have lists of different numeric types, for example: QList<qreal>
I have simple drag and drop functions implemented in QmainWindow the reference taken from
Pretty new to threading and I have this QList that the threads share between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.