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 4573494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:47:34+00:00 2026-05-21T19:47:34+00:00

I am using a QList and I want to iterator though it, but the

  • 0

I am using a QList and I want to iterator though it, but the iterator that i get from it keeps failing, even if I use the auto keyword or not.

for( auto iter = myModel->getList().begin(); iter != myModel->getList().end(); ++iter )
{
    if( (*iter)->getList().empty() )
    {
        if( (*iter)->getData()->getCode() == baseData->getCode() )
            return true;
    }
}
return false;

Does anyone know why this could be happening? Or is there a different way that I am to use the QList::iterator?

EIDT:
I have noticed another odd thing about this. I do see that the iterator is showing (error)0, and this is odd because it actually contains the data I need.

But if I edit my code to look like this:

QList<T*> tempList = myModel->getList();    
for( auto iter = tempList .begin(); iter != tempList .end(); ++iter )
        {
            if( (*iter)->getList().empty() )
            {
                if( (*iter)->getData()->getCode() == baseData->getCode() )
                    return true;
            }
        }
        return false;

then everything works fine. I guess it is a solution, but does anyone know why it needs to be done like this? Or am I just not seeing something very important?

  • 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-05-21T19:47:34+00:00Added an answer on May 21, 2026 at 7:47 pm

    I see two issues with the code.

    a) myModel->getList() returns a copy of your QList declared something like this:

    QList<T*> YourModel::getList()
    

    you actually should get an access violation. But I don’t know if that’s what happening.

    b) If you only rely on what Visual Studio is saying about iter, forget it, its wrong. It just can’t handle the iterator. Allthough the iterator says its (error)0 as an int it will still give you the correct data if dereferenced.

    If none of the above is the case please specify what actually goes wrong, is the for loop beeing skipped all together? If so, I’ll edit my answer accordingly.

    Best regards
    D

    EDIT
    The reason why it works fine with the temporary List object is following:
    The moment you create an iterator with the begin() method of the list, the list detaches its private data (implicit sharing), meaning it creates a deep copy of itself because you could change the data with that iterator. Without the temporary object myModel->getList() creates a temporary object which immediately after the iterator has been created is beeing destroyed. From that moment on the iterator points to nowhere. You might try using the const iterator, this one should work without detaching the private data hence without the problem.
    Another approach would be to make your model return the list as a const reference or a pointer.
    Another way would be to discard iterators and use the at() method of the list directly. Makes code more readable in my opinion.

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

Sidebar

Related Questions

I'm trying to free memory after using QList, but it doesn't seem to work
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active
I want to save a QList<int> to my QSettings without looping through it. I
I want to implement a multithreading environment using Qt4. The idea is as follows
I'm using Qt 4.5 and im working with a QList<QStringList> which is a list
I'm trying to append items to a QList at runtime, but I'm running on
I'm using a rather complex QList in a derivation of QAbstractTableModel to store data:

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.