Q更改当前索引时不选择
QCombobox doesn't select when changing currentIndex
在我的构造函数中,我连接到Sqlite数据库并从中读取"Categories"(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
}
我的QCombox中有所有项目,但没有一个被选中。我必须点击框并自己选择一个。这是不应该发生的。
怎么了?为什么它不自己选择一个呢?
编辑:
currentIndexChanged
信号:
void MainWindow::on_cmbCategory_currentIndexChanged(int index){
currCategoryIndex = index;
}
也许第一项是空的?由于您只向QComboBox添加项,因此索引0(从一开始)可能是一个空字符串。
试试推杆ui->cmbCategory->removeItem(0);
在updateCategories的开头检查是否是
此外,如果currCategoryIndex是一个不存在的索引(例如-1),则QComboBox也将为空(即使没有空字符串可供选择)-在这种情况下,您可以尝试在函数中硬编码0(如果您希望该项始终是第一个),或添加额外的检查,例如:
if (0 > currentCategoryIndex || currentCategoryIndex > ui->cmbCategory->count())
currentCategoryIndex = 0
相关文章:
- 如何使用默认参数等选择模板专业化
- 数组索引的值没有增加
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 选择要调用的构造函数
- 选择前缀数量最多的索引
- 从动态索引中选择ConstexPR索引
- 使用查找表选择具有运行时索引的可变参数类型
- 张量征服,我如何选择应收缩的索引
- 正确复制 QTableView 中的选择(模型索引问题)
- 使用值为 std::shared_ptr的映射是具有多索引类列表的良好设计选择
- 从STL矢量的真值中随机选择索引
- 如何在C++中按索引选择字符串中的特定字符
- 选择排序获取错误的数组索引
- Q更改当前索引时不选择
- 使用索引从字符串中选择单个字符
- 选择多列索引查询优化
- 构造由索引从类列表中选择的类,c++
- 如何从另一个仅静态选择满足特定类型规则的索引的元组实例创建元组实例
- CUB使用返回的索引选择它