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?
#ifndef __IMAGE_DELEGATE_H_
#define __IMAGE_DELEGATE_H_
#include <QItemDelegate>
{
Q_OBJECT
public:
};
#endif
#ifndef __IMAGE_DELEGATE_H_
#define __IMAGE_DELEGATE_H_
#include <QItemDelegate>
class SQLQueryItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
SQLQueryItemDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
virtual QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};
#endif
To copy to clipboard, switch view to plain text mode
#include "SQLQueryItemDelegate.h"
#include <QPainter>
{
{
siImage.fromData((const uchar*) ba.data(), ba.size());
siPixmap.fromImage(siImage,Qt::AutoColor);
painter->drawPixmap(option.rect, siPixmap);
}
else
{
}
}
{
{
}
else
{
}
}
#include "SQLQueryItemDelegate.h"
#include <QPainter>
void SQLQueryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QVariant &v = index.data();
if (v.type()==QVariant::ByteArray)
{
const QByteArray ba = v.toByteArray();
QImage siImage(320,200,QImage::Format_ARGB32);
siImage.fromData((const uchar*) ba.data(), ba.size());
QPixmap siPixmap;
siPixmap = QPixmap(320,200);
siPixmap.fromImage(siImage,Qt::AutoColor);
painter->drawPixmap(option.rect, siPixmap);
}
else
{
QItemDelegate::paint(painter,option,index);
}
}
QSize SQLQueryItemDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QVariant &v = index.data();
if (v.type()==QVariant::ByteArray)
{
return QSize(320,200);
}
else
{
return QItemDelegate::sizeHint(option,index);
}
}
To copy to clipboard, switch view to plain text mode
Thanks, everyone is very helpful here.
Bookmarks