我QStyledItemDelegate在我的模型上为特定领域设置一个,并QComboBox从QStyledItemDelegate::createEditor
QComboBox* createEditor(QWidget* parent)
{
QComboBox* cb = new QComboBox(parent);
cb->addItem("UNDEFINED");
cb->addItem("TEST");
cb->addItem("OSE");
cb->addItem("TSE");
return cb;
}
void setEditorData(QWidget* editor, const QModelIndex& index)
{
QComboBox* cb = qobject_cast<QComboBox*>(editor);
if (!cb)
throw std::logic_error("editor is not a combo box");
QString value = index.data(Qt::EditRole).toString();
int idx = cb->findText(value);
if (idx >= 0)
cb->setCurrentIndex(idx);
cb->showPopup();
}
这工作正常,当我选择有问题的字段时,我会看到一个组合框。
当我从下拉列表中选择一个选项时,组合框会关闭,并且该项目旁边会显示一个下拉图标:
此时我希望QStyledItemDelegate::setModelData调用该函数,以便在列表中选择一个项目将数据提交给模型。
但是,我需要先按Enter提交数据(下拉图标消失)
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index)
{
QComboBox* cb = qobject_cast<QComboBox*>(editor);
if (!cb)
throw std::logic_error("editor is not a combo box");
model->setData(index, cb->currentText(), Qt::EditRole);
}
问题:
当用户选择列表中的一个项目并且组合框列表关闭时,我如何配置我QComboBox以自动Enter提交数据,而不是需要额外按下?


