gpt4 book ai didi

qt - QComboBox - 不选择任何条目

转载 作者:行者123 更新时间:2023-12-03 13:01:03 26 4
gpt4 key购买 nike

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

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

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

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

我已经尝试使用 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不允许选择“无项目”的状态。选择必须始终有效。

您必须在第一个位置添加一个空字符串项,您可能需要检查此主题以使此虚拟项不可选择:https://stackoverflow.com/a/7633081/3336423

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

关于qt - QComboBox - 不选择任何条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35113892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com