PDA

View Full Version : QComboBox Assert Mistake



jfe
30th July 2007, 10:02
I have a filled QComboBox, I want to clear it and fill it again. I do that with the following code:

// clear the comboBox
ui.staHealthNameCombo->clear();

// put the QStringList to the Combobox
ui.staHealthNameCombo->addItems(newAssuredComboStringList);

When calling the addItems function I always get :

ASSERT failure in QList<T>:: at:"index out of range" in c:\...\qt4.2.2\...\qlist.h

The problem is obviously an out of range error when putting the qStringlist into the combobox. However, I get the same error when I use the addItem - Function that only uses a qstring as an argument.

Could this be an error in the QT API?

Thanks for your help.

marcel
30th July 2007, 10:05
Could this be an error in the QT API?

Highly improbable.

How do you fill that string list?

Regards

jfe
30th July 2007, 11:18
This is the code of filling the Stringlist:

QString insuranceName;
// append all InsuranceNames from EDCHEALTHINSURANCE
for (int i=0; i<allhealthinsurances.size(); ++i)
{
insuranceName = allhealthinsurances.at(i)->assuredname();

newAssuredComboStringList << insuranceName;
}

marcel
30th July 2007, 11:25
What does allhealthinsurances.at(i)->assuredname() returns? Is it a copy of a QString or exactly its QString, the one it contains?
Maybe you destroy allhealthinsurances( along with its contents), and the string list becomes invalid by the time you add it to the combo. But this only if it is a reference to the original one.

What if you try to add the items in the combo one by one, and not the whole list at once? What happens then?

Regards

marcel
30th July 2007, 11:40
Can you post a minimal version of the code that compiles and also reproduces the problem?

Regards

jfe
30th July 2007, 11:56
// fill ComboBox again
// get all Healthinsurances
QList<RefPointer<HealthInsurance>> allhealthinsurances;
healthinsuranceDao.loadAllByPatientID(allhealthins urances, patient->id());

QStringList newAssuredComboStringList;

QString insuranceName;
// append all InsuranceNames from EDCHEALTHINSURANCE
for (int i=0; i<allhealthinsurances.size(); ++i)
{
insuranceName = allhealthinsurances.at(i)->assuredname();

newAssuredComboStringList << insuranceName;
}
ui.staHealthNameCombo->clear();
// put the QStringList to the Combobox
ui.staHealthNameCombo->addItems(newAssuredComboStringList);

The code is the same as I posted above.

When I put use the function ui.staHealthNameCombo->addItem(insuranceName) in the for loop I get the same result.
insuranceName is a QString and Dao Object->assuredname() also returns a QString so that is not the problem.

If I debug the program I have the right values in the variables.
I also tried printing out the number of values in the comboBox before and after clearing, but they are correct.

:confused:

marcel
30th July 2007, 12:08
Are you sure the ASSERT isn't raised here?


for (int i=0; i<allhealthinsurances.size(); ++i)
{
insuranceName = allhealthinsurances.at(i)->assuredname();

newAssuredComboStringList << insuranceName;
}


Because it makes no sense to get an index out of bounds for a pre-filled string list.
Unless the memory gets corrupted somehow.
Anyway, everything so far looks like memory violations to me.

If you get the same error if you fill the combo in the above for loop, the the error isn't in the string list. More likely in allhealthinsurances. Are you positive this is filled correctly.

What is RefPointer?

It is pretty hard to find the problem like this.
But debugging it carefully should reveal the problem.

Regards

jfe
30th July 2007, 12:44
Are you sure the ASSERT isn't raised here?


for (int i=0; i<allhealthinsurances.size(); ++i)
{
insuranceName = allhealthinsurances.at(i)->assuredname();

newAssuredComboStringList << insuranceName;
}



Yes I am very sure!



Because it makes no sense to get an index out of bounds for a pre-filled string list.
Unless the memory gets corrupted somehow.
Anyway, everything so far looks like memory violations to me.

If you get the same error if you fill the combo in the above for loop, the the error isn't in the string list. More likely in allhealthinsurances. Are you positive this is filled correctly.


I use allhealthinsurances a couple of times before. I works perfectly.




It is pretty hard to find the problem like this.
But debugging it carefully should reveal the problem.


I know. But for me too right now ;)
Thanks for your hints, I will try debugging it again.

marcel
30th July 2007, 13:00
If the problem is indeed in addItems/addItem after a clear() is issued, then could you try with insertItem(-1, insuranceName)?

This always prepends the item to the item list.,

Regards