QComboBox - Select 没有条目

QComboBox - Select no entry

我的 ui 上有一个 QComboBox 并像这样设置模型:

QStringListModel* model = new QStringListModel;
QStringList stringlist;
stringlist << "Test1" << "Test2" << "Test3";

model->setStringList(stringlist);
ui->comboBox->setModel(model);

现在我想将当前索引更改为 none(以便我得到一个空白组合框)。

我已经尝试使用 ui->comboBox->setCurrentIndex(-1); 将当前索引设置为 -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

常规(不可编辑)QComboBox 不允许选择 "no item" 的状态。选择必须始终有效。

您必须在第一个位置添加一个空字符串项,并且您可能需要查看此主题以使该虚拟项不可选:


编辑:实际上,看起来完全有可能将任何组合框(可编辑或不可编辑)的选择设置为 -1。所以没有必要按照上面的建议添加一个空项。