App crashes clearing QComboBox
-
I have an app that queries a database for last names then populates a combobox. When the user selects a last name the first name combobox is supposed to clear and load first names that go with the selected last name. The first time through when the first name combo box is empty the clear has no effect. The second time through the app crashes. Both comboboxes are tied to slots so the clear firstname action in the lastname combobox callback is triggering the firstname slot while the lastname slot is still active. How can we clear the combo box without causing the crash. The code is shown below
void GcfProjects::on_mLastNameList_currentIndexChanged(int index)
{
ui->mFirstNameList->clear();if (index >= 0) { QString name = ui->mLastNameList->itemText(index); QString errMsg; char tempBuf[255]; QSqlQuery *searchQuery = new QSqlQuery(mSqlDatabase); std::string dbTableName = "gcf_2016."; dbTableName += mProjectToDbTableMap[mCurrentSpieces]; QComboBox box; if (searchQuery != NULL) { // if (dbTableName != NULL) { sprintf(tempBuf, "SELECT distinct first_name FROM %s " "where last_name = '%s'", dbTableName.c_str(), ui->mLastNameList->currentText().toStdString().c_str()); searchQuery->prepare(tempBuf); if (!searchQuery->exec()) { QSqlError sqlError = searchQuery->lastError(); errMsg = sqlError.databaseText(); qDebug() << errMsg; } else { if (searchQuery->isActive()) { while (searchQuery->next()) { QString name = searchQuery->record().value(0).toString(); ui->mFirstNameList->addItem(name); } searchQuery->finish(); } } } // delete searchQuery; } }
}
void GcfProjects::on_mFirstNameList_currentIndexChanged(int index)
{
if (index >= 0)
{
QString name = ui->mLastNameList->itemText(index);
}}
-
@bclay
Not exactly an answer to your question, butQComboBox box;
is a local variable (and you don't seem to use it).
Also this:char tempBuf[255]; // ... sprintf(tempBuf, "SELECT distinct first_name FROM %s " "where last_name = '%s'", dbTableName.c_str(), ui->mLastNameList->currentText().toStdString().c_str());
is just terrible!
Use theQString
's API, which is also type safe and doesn't require all those type conversions. E.g:QString query = QStringLiteral("SELECT distinct first_name FROM %1 where last_name = '%2'").arg(dbTableName).arg(ui->mLastNameList->currentText());
Or better yet, bind your variables to the query (as described in the docs) so they will also be properly escaped.
-
Sorry to offend your senses. I have been coding for 35 years and mostly in C or C++. I have been coding Qt apps for only a year so the old school stuff comes out a lot.
the "QComboBox box" is left over from some debugging and is not actually used at all. The app was created in QtCreator so the ui->mFirstNameList is specified in the auto-generated code.
Still the question is how to get past the crash when I call clear on the combobox. I saw at one point something about disable but disabling the ui->mFirstNameList does not change anything and I do not see a way to get to the action to disable that. ui->mFirstNameList.action returns an empty list.
-
Sorry to offend your senses.
Not at all, I'm not that sensitive. :)
Still the question is how to get past the crash when I call clear on the combobox.
What do you get? Is it a failed assertion, segmentation fault? Would you provide a stack trace for the crash?
Kind regards.
-
-
I found what I was looking for but that did not fix the issue either. Basically I wrapped the calls to clear in blockSignal calls such as the following
bool oldState = ui->mLastNameList->blockSignals(true); ui->mLastNameList->clear(); ui->mLastNameList->blockSignals(oldState);
Even though the signal was blocked the app still crashed then clear was called.
Thanks for your suggestions.
Bruce
-
When clear() function is called, currentIndexChanged(int index) function also will be called with index = -1. So if you uses index at some variables, it cause the memory faults. So you have to add the following code at the very first line.
if(index <0) return;
I hope this will be helpful.