To QTableWidget, I derive paint() function of QItemDelegate like this:
{
painter->save();
QFont font
= qvariant_cast<QFont>
(index.
data(Qt
::FontRole));
painter->setFont(font);
painter->drawStaticText(option.rect.x(),option.rect.y(), QStaticText(index.data().toString()));
painter->restore();
}
void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
painter->save();
QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
painter->setFont(font);
painter->drawStaticText(option.rect.x(),option.rect.y(), QStaticText(index.data().toString()));
painter->restore();
}
To copy to clipboard, switch view to plain text mode
_ Draw rich text with Qt::AlignCenter:
{
QFont font
= qvariant_cast<QFont>
(index.
data(Qt
::FontRole));
QBrush brush
= qvariant_cast<QBrush>
(index.
data(Qt
::ForegroundRole));
QRect boundingRect
= fm.
boundingRect(index.
data().
toString().
remove("<sub>").
remove("</sub>"));
//We can remove other html tags int stringWidth = boundingRect.width();
int stringHeight = boundingRect.height();
int xPoint = option.rect.x() + option.rect.width()/2 - stringWidth/2;
int yPoint = option.rect.y() + option.rect.height()/2 - stringHeight/2;
painter->save();
painter->setFont(font);
painter->setPen(brush.color());
painter->drawStaticText(xPoint , yPoint, QStaticText(index.data().toString()));
}
void TableWidgetItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QFont font = qvariant_cast<QFont>(index.data(Qt::FontRole));
QBrush brush = qvariant_cast<QBrush>(index.data(Qt::ForegroundRole));
QFontMetrics fm(font);
QRect boundingRect = fm.boundingRect(index.data().toString().remove("<sub>").remove("</sub>")); //We can remove other html tags
int stringWidth = boundingRect.width();
int stringHeight = boundingRect.height();
int xPoint = option.rect.x() + option.rect.width()/2 - stringWidth/2;
int yPoint = option.rect.y() + option.rect.height()/2 - stringHeight/2;
painter->save();
painter->setFont(font);
painter->setPen(brush.color());
painter->drawStaticText(xPoint , yPoint, QStaticText(index.data().toString()));
}
To copy to clipboard, switch view to plain text mode
Regards.
Bookmarks