PDA

View Full Version : How to get Icons in QTableWidgetItem to take up the whole item space



Slewman
24th February 2010, 23:40
Posted something about this earlier... but it kinda got lost in the posts... so im posing the question again with a better title haha.

i have written a custom item delegate... but it doesnt seem to work



class MyItemDelegate: public QItemDelegate
{
public:
MyItemDelegate(QObject* pParent = 0) : QItemDelegate(pParent)
{
}
void MyItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
drawBackground(painter, option, index);
drawDecoration(painter, option, option.rect, icon.pixmap(option.rect.size()));
drawFocus(painter, option, option.rect);
}
};


anyone have any ideas? im real desperate to get this thing working, ive been working on it for days now and i dont know where else to look

Slewman
25th February 2010, 14:26
seriously though... anyone see anything obvious that i am not doing? im going insane over here... its like im taking crazy pills!

Slewman
26th February 2010, 16:16
got something to work... not perfect, but the icon will now keep aspect ration in the item. enjoy!!!




#include <QStyledItemDelegate>
#include <QPainter>

class ItemDelegate : public QStyledItemDelegate
{
public:
enum ItemDataRole { SubTextRole = Qt::UserRole + 100 };

ItemDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) {}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
void paint(QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QString line1 = index.data(Qt::DisplayRole).toString();
//QString line2 = index.data(SubTextRole).toString();

int textW = option.fontMetrics.width(line1);
QSize iconSize = icon.actualSize(option.decorationSize);

return QSize(iconSize.width() + 4,
iconSize.height() + 2 + option.fontMetrics.lineSpacing() * 2 + 4);
}

void ItemDelegate::paint(QPainter *p, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
p->save();

QString line1 = index.data(Qt::DisplayRole).toString();
QString line2 = index.data(SubTextRole).toString();

QStyleOptionViewItemV4 opt(option);
initStyleOption(&opt, index);

QStyle *style = opt.widget->style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, p, opt.widget);

if (option.state & QStyle::State_Selected)
p->setPen(QPen(option.palette.brush(QPalette::Highlig htedText), 0));

QRect itemRect = option.rect.adjusted(2, 2, -2, -2);
QRect r = QStyle::alignedRect(opt.direction, Qt::AlignHCenter | Qt::AlignLeft, itemRect.size(), itemRect);
opt.icon.paint(p, r);

int h = option.fontMetrics.lineSpacing();
QRect textRect(itemRect.left(), itemRect.bottom() - h, itemRect.width(), h);

p->drawText(textRect, Qt::AlignVCenter | Qt::AlignHCenter, line1);

QColor subTextColor = p->pen().color();
subTextColor.setAlphaF(.5);

p->restore();
}