Hamdi Rakkez
Hamdi Rakkez

Reputation: 79

QStandardItem issue

i'm having a problem with a Qlist of QStandardItems when i want to fill this QList. This is the Error message from Qt Creator : ASSERT failure in QList::operator[]: "index out of range"

This is My code but it's temporary, the img will change every iteration.

for(int i=0;i<4;i++)
    {
        QList <QStandardItem*> itemCountryFlagTab;
        QImage img =QImage(":/country/DataBase/country_flags/us.gif");
        itemCountryFlagTab.reserve(5);
        itemCountryFlagTab[i]->setData(QVariant(QPixmap::fromImage(img)), Qt::DecorationRole);
        modelTraceRoute.setItem(i, 4, itemCountryFlagTab[i]);
    }

Upvotes: 0

Views: 272

Answers (1)

KjMag
KjMag

Reputation: 2770

You don't initialize itemCountryFlagTab before using it, and thus you refer to the elements it doesn't have, and that is the source of your error. Also, you are creating itemCountryFlagTab from scratch during each loop iteration, so even if you initialized it, the results would be lost after the end of each loop iteration.

In short: apart from initializing the mentioned variable, you should also put itemCountryFlagTab declaration outside of the loop, if you want it not to be reset/vanish after every iteration.

The reserve() function is not used to initialize variables - it just allocates space for them so that the data of QList would not need to be reallocated over and over again in case you know how many elements are going to be appended to the list.

Upvotes: 2

Related Questions