I think I found the problem.
First, make sure the view in the QComboBox
allows editing:
cb->view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
I'm not sure if that's a good practice, but that was the only way I could make it work. The default value for editTriggers
is QAbstractItemView::NoEditTriggers
Second, make sure your model allows editing:
Qt::ItemFlags MyModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool MyModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
// Modify data..
emit dataChanged(index, index);
return true;
}
return false;
}
It has a problem, though. The first time you see the ComboBox
, you can change the current item text, and it won't call the delegate methods for editing. You have to select one item and then you'll be able to edit it.
Anyway, I find that using a QComboBox
for editable items is counter-intuitive. Are you sure that you need a QComboBox
for this task?
Hope it helps