在我的构造函数中,我连接到 Sqlite 数据库并从中读取“类别”(QString
s)。我将它们存储在一个QList
. 我通过调试器检查它们是否为空,但一切都很好。
在我的int currCategoryIndex
初始化列表中设置为 0。由于QComboboxes
从 0 开始索引,它应该是第一项。
我的构造函数的摘录:
dm.readDB_C(c_list); //reads into c_list
if(c_list.count() > 0) //has 1 or more items
updateCategories();
这是我读取数据库的部分,检查它是否为空,如果没有调用将这些类别添加到QComboBox
.
updateCategories()
功能 :
void MainWindow::updateCategories()
{
for(int i = 0; i < c_list.count(); i++){
if(ui->cmbCategory->findText(c_list[i]) != -1){ //-1 means "not found"
continue;
} else {
ui->cmbCategory->addItem(c_list[i]); //Add to QCombobox
}
}
ui->cmbCategory->setCurrentIndex(currCategoryIndex); //Should be the first item
}
我的 QCombobox 中有所有项目,但没有选择任何项目。我必须单击该框并自己选择一个。这不应该发生。
怎么了?为什么它不自己选择一个?
编辑:
currentIndexChanged
信号:
void MainWindow::on_cmbCategory_currentIndexChanged(int index){
currCategoryIndex = index;
}