June 21, 2011

Mr.G Mr.G
Lab Rat
44 posts

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:

  1. void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  2.  const QModelIndex &index) const {
  3.  int value = index.model()->data(index, Qt::DisplayRole).toInt();
  4.  switch(value){
  5.  case MyTypes::Type_A:
  6.   //change displayed value to "type A"
  7.   break;
  8.   //.... and so on
  9.  };
  10. }

is there a way to do this?
thx,
G

3 replies

June 21, 2011

Volker Volker
Robot Herder
5428 posts

I think it would be better to put an QAbstractProxyModel subclass inbetween (or maybe a QSortFilterProxyModel subclass, you’ll have to check which one fits best).

June 21, 2011

Andre Andre
Area 51 Engineer
6031 posts

For Qt 4.7, use a QSortFilterProxyModel as your base, for 4.8, a QIdentityProxyModel. You don’t want to implement the item mapping yourself, I think.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

June 21, 2011

Mr.G Mr.G
Lab Rat
44 posts

well, I solved it by using the painter:

  1. void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  2.  const QModelIndex &index) const {
  3.  int value = index.model()->data(index, Qt::DisplayRole).toInt();
  4.  QString text;
  5.  switch(value){
  6.  case MyTypes::Type_A:
  7.  text =  "type A";
  8.  break;
  9.  //.... and so on
  10.  };
  11.  QRect r = option.rect.adjusted(2, 2, -2, -2);
  12.  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

 
  ‹‹ [SOLVED] how to parse character pointer;      [SOLVED]display image through http url ››

You must log in to post a reply. Not a member yet? Register here!