QAbstractItemDelegate::paint() : custom Delegate, how to alter the value displayed in cell - Solved
-
hello all,
there is probably an easy way to do this: the thing is that in the DB the values of a 'mytype' column are from 1 to 5. these
values represent constants in the program, so, here is what I have:
@
void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
int value = index.model()->data(index, Qt::DisplayRole).toInt();
switch(value){
case MyTypes::Type_A:
//change displayed value to "type A"
break;
//.... and so on
};
}
@is there a way to do this?
thx,
G -
well, I solved it by using the painter:
@
void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QString text;
switch(value){
case MyTypes::Type_A:
text = "type A";
break;
//.... and so on
};
QRect r = option.rect.adjusted(2, 2, -2, -2);
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text, &r);
@it is a quick and dirty solution but will do for the time table I have for this, a proxy model might be a better solution, but, the delegate also handles the editing part of the cell, so, it's a good 'all in one' solution