PDA

View Full Version : delegates



hgedek
24th October 2007, 11:36
I am learning QTableWidget.I wanted to show only images at QTableWidgetItems.So I tried to add labels to tablewidget cells.(there is an example:QSpinBox).
Is there another way of this:Because if I use QTableWidgetItem ->icon() function;I can still show a linedit in cells.But I want only images.

jpn
24th October 2007, 17:16
Do you mean that you would like icons to fill whole cell?

hgedek
24th October 2007, 18:18
yes.all cell will show image.

jpn
24th October 2007, 19:00
It should be as simple as subclassing QItemDelegate and reimplementing QItemDelegate::paint() more or less in the following way:


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);
}

Then set the delegate to your table widget and use QTableWidgetItem::setIcon() to set icons.