Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on adding to empty comboBox in Qt

Tags:

combobox

qt

I use Qt 5.2.0 (MSVC 2010).

I added to my form in Qt a ComboBox.

Then I want to fill it with numbers:

for (i = 0; i < n; i++){
    ui->tableCombo->addItem(QString::number(i));
}

When I add a first element right in the form, it successfully adds numbers. But when I leave it empty, it throws an error:

ASSERT failure in QVector::operator[]: "index out of range"

Debugger shows that error occured right in this line. And there is no QVector across the line.

After adding qDebug().

qDebug() << "readFileToStringList: msg10";
for (i = 0; i < n; i++){
    qDebug() << "readFileToStringList: msg20  i = " << i;
    ui->tableCombo->addItem(QString::number(i+1));
    qDebug() << "readFileToStringList: msg30";
}
qDebug() << "readFileToStringList: msg40";

I get the same result

readFileToStringList: msg10 
readFileToStringList: msg20  i =  0 
ASSERT failure in QVector<T>::operator[]: "index out of range", file C:\Qt\Qt5.2.0\5.2.0\mingw48_32\include/QtCore/qvector.h, line 369
like image 495
Michael Avatar asked Dec 28 '25 19:12

Michael


1 Answers

I had this exact problem and couldn't figure it out for a couple hours. I realized ::addItem() was triggering the indexChanged(int) signal, which I had connected to a function that was causing an out-of-range error in a container.

I would say it was possibly the problem here too, but I'm sure the OP has moved on since then. To me it isn't exactly intuitive that the indexChanged signal would be called on insertion of new items, since it doesn't actually change the currentIndex.

Hopefully if anyone else gets tripped up this will help them!

like image 193
Michael B. Avatar answered Dec 31 '25 09:12

Michael B.