How to get Icons in QTableWidgetItem to take up the whole item space
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
Code:
{
public:
{
}
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
Re: How to get Icons in QTableWidgetItem to take up the whole item space
seriously though... anyone see anything obvious that i am not doing? im going insane over here... its like im taking crazy pills!
Re: How to get Icons in QTableWidgetItem to take up the whole item space
got something to work... not perfect, but the icon will now keep aspect ration in the item. enjoy!!!
Code:
#include <QStyledItemDelegate>
#include <QPainter>
class ItemDelegate : public QStyledItemDelegate
{
public:
enum ItemDataRole { SubTextRole = Qt::UserRole + 100 };
ItemDelegate
(QObject *parent
= 0) : QStyledItemDelegate
(parent
) {}};
{
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);
}
{
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::HighlightedText),
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();
}