{
painter
->setRenderHint
(QPainter::TextAntialiasing);
if (option.
state==QStyle::State_Selected) painter->setBrush(option.palette.highlight());
else
painter
->setBrush
(QBrush(Qt
::white));
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=sizeHint(option,index).width(), height;
QImage img
=static_cast<rosterItem
*>
(index.
internalPointer())->image
();
QRect irect
(option.
rect.
x(), option.
rect.
y()+2, img.
width(), img.
height());
QRect nrect
(option.
rect.
x()+img.
width()+2, option.
rect.
y()+2, width
-img.
width()-2, fmetrics.
height());
painter->drawImage(irect, img);
// CONTACT NAME
painter->drawText(nrect, Qt::AlignLeft|Qt::AlignTop, elidedText(fmetrics, nrect.width(),Qt::ElideRight,item->data(0)));
if(!item->data(1).isEmpty())
{
int height=fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
if(fmetrics.width(item->data(1))%width>0)
height+=fmetrics.height();
QRect drect
(option.
rect.
x()+1, option.
rect.
y()+img.
height()+2, width, height
);
// DESCRIPTION
painter->drawText(drect, Qt::TextWordWrap|Qt::AlignLeft|Qt::AlignTop, item->data(1));
}
}
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=option.rect.width(), height;
QImage img
=static_cast<rosterItem
*>
(index.
internalPointer())->image
();
if(item->data(1).isEmpty())
{
height=img.height()+2;
}
else
{
height=img.height()+2+fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
if(fmetrics.width(item->data(1))%width>0)
height+=fmetrics.height();
}
qDebug("w:%d h:%d", width, height);
return QSize(width, height
+2);
}
void rosterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->setRenderHint(QPainter::TextAntialiasing);
if (option.state==QStyle::State_Selected)
painter->setBrush(option.palette.highlight());
else
painter->setBrush(QBrush(Qt::white));
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=sizeHint(option,index).width(), height;
QFontMetrics fmetrics=option.fontMetrics;
QImage img=static_cast<rosterItem*>(index.internalPointer())->image();
QRect irect(option.rect.x(), option.rect.y()+2, img.width(), img.height());
QRect nrect(option.rect.x()+img.width()+2, option.rect.y()+2, width-img.width()-2, fmetrics.height());
painter->drawImage(irect, img);
// CONTACT NAME
painter->drawText(nrect, Qt::AlignLeft|Qt::AlignTop, elidedText(fmetrics, nrect.width(),Qt::ElideRight,item->data(0)));
if(!item->data(1).isEmpty())
{
int height=fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
if(fmetrics.width(item->data(1))%width>0)
height+=fmetrics.height();
QRect drect(option.rect.x()+1, option.rect.y()+img.height()+2, width, height);
// DESCRIPTION
painter->drawText(drect, Qt::TextWordWrap|Qt::AlignLeft|Qt::AlignTop, item->data(1));
}
}
QSize rosterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=option.rect.width(), height;
QFontMetrics fmetrics=option.fontMetrics;
QImage img=static_cast<rosterItem*>(index.internalPointer())->image();
if(item->data(1).isEmpty())
{
height=img.height()+2;
}
else
{
height=img.height()+2+fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
if(fmetrics.width(item->data(1))%width>0)
height+=fmetrics.height();
}
qDebug("w:%d h:%d", width, height);
return QSize(width, height+2);
}
To copy to clipboard, switch view to plain text mode
Bookmarks