I have the following code that should display any column that is a byte array as a 320x200 tga image. But the cells in the QTTableView are grey rather than the image. Also they don't size to 320x200, as the sizeHint() function is never called.

Any idea what I'm doing wrong?

Qt Code:
  1. #ifndef __IMAGE_DELEGATE_H_
  2. #define __IMAGE_DELEGATE_H_
  3.  
  4. #include <QItemDelegate>
  5.  
  6. class SQLQueryItemDelegate : public QItemDelegate
  7. {
  8. Q_OBJECT
  9. public:
  10. SQLQueryItemDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
  11.  
  12. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
  13. const QModelIndex &index) const;
  14.  
  15. virtual QSize sizeHint(const QStyleOptionViewItem &option,
  16. const QModelIndex &index) const;
  17. };
  18.  
  19. #endif
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "SQLQueryItemDelegate.h"
  2. #include <QPainter>
  3.  
  4. void SQLQueryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  5. const QModelIndex &index) const
  6. {
  7. const QVariant &v = index.data();
  8. if (v.type()==QVariant::ByteArray)
  9. {
  10. const QByteArray ba = v.toByteArray();
  11. QImage siImage(320,200,QImage::Format_ARGB32);
  12. siImage.fromData((const uchar*) ba.data(), ba.size());
  13. QPixmap siPixmap;
  14. siPixmap = QPixmap(320,200);
  15. siPixmap.fromImage(siImage,Qt::AutoColor);
  16. painter->drawPixmap(option.rect, siPixmap);
  17.  
  18. }
  19. else
  20. {
  21. QItemDelegate::paint(painter,option,index);
  22. }
  23. }
  24.  
  25. QSize SQLQueryItemDelegate::sizeHint(const QStyleOptionViewItem &option,
  26. const QModelIndex &index) const
  27. {
  28. const QVariant &v = index.data();
  29. if (v.type()==QVariant::ByteArray)
  30. {
  31. return QSize(320,200);
  32. }
  33. else
  34. {
  35. return QItemDelegate::sizeHint(option,index);
  36. }
  37. }
To copy to clipboard, switch view to plain text mode 

Thanks, everyone is very helpful here.