我的ui上有一个QComboBox并按如下所示设置模型:
QStringListModel* model = new QStringListModel;
QStringList stringlist;
stringlist << "Test1" << "Test2" << "Test3";
model->setStringList(stringlist);
ui->comboBox->setModel(model);
Run Code Online (Sandbox Code Playgroud)
现在,我想将当前索引更改为none(以便获得一个空白的组合框)。
我已经尝试使用将当前索引设置为-1 ui->comboBox->setCurrentIndex(-1);,但结果是在qlist中超出范围表达式的索引:
ASSERT failure in QList<T>::operator[]: "index out of range", file F:/Qt/5.4/mingw491_32/include/QtCore/qlist.h, line 486
Run Code Online (Sandbox Code Playgroud)
常规(不可编辑) QComboBox不允许选择“无项目”的状态。选择必须一直有效。
您将必须在第一个位置添加一个空字符串项目,并且您可能需要检查此主题以使该虚拟项目无法选择:https : //stackoverflow.com/a/7633081/3336423