how to avoid switch statements in item delegates
Hi
I am implementing a Item Delegate for my tree based model.
I need to create a different editor depending upon the item being edited.
At the moment I am using switch statements to decide which editor to supply, but as my model grows so do the switch statements.
Code:
QModelIndex parentIndex
= index.
model()->parent
(index
);
switch (index.model()->parent(index).row())
{
case 0:
{
switch (index.row())
{
case 0:
case 1:
{
}
break;
case 2:
{
Common::Enumerations::surveyUnits().getDisplayStrings(list);
}
break;
}
}
break;
case 1:
{
}
break;
}
return editor;
Is there a better way to achieve this?
TIA
Re: how to avoid switch statements in item delegates
How do you put the data into the model?
If it's a derivative of QAbstractItemModel you could use setData with your own DataRole to save a value from an enum, something like
Code:
enum EditorType {
LineEditEditor,
ComboBoxEditor,
...
}
Then you only need one switch