PDA

View Full Version : QStandardItem : subpart of the text as bold



titoo
7th February 2007, 15:40
how to bold a part of the text of a QStandardItem ?
atm i see that i can choose the font of the whole text, is this possible to change only a part of the font of the text ?

wysota
7th February 2007, 17:54
I take it that you ask how to bold a fragment of data in the view... The answer is to provide a custom delegate that can handle rich text.

titoo
9th February 2007, 09:55
i then need to create a qtextedit when i enter in QItemDelegate:: paint(...) ?

wysota
9th February 2007, 11:28
No. Use QTextLayout or something simmilar... I did it like this:


void CommentDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QTextDocument doc;
doc.setHtml( index.data().toString() );
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize( option.rect.size());
painter->translate(option.rect.x(), option.rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
}

Georgest
5th October 2008, 08:35
Need to add a line:

painter->setClipRect(option.rect);
before painter->translate(...)
to avoid drawing text lines outside of the cell.
And highlight a focus cell changing background colour of text or with fillRect():

if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
And change colour of highlighted text.
For example:

void MyDelegate::paint (
QPainter *painter,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const {

QString text;
QRect rect;
QVariant value;
QStyleOptionViewItemV4 opt = setOptions(index, option); // or V2 for Qt4.2

//value = index.data(Qt::DisplayRole);
//text = value.toString();
//opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
//drawDisplay(painter, opt, opt.rect, text);

// Change line style around of the cell:
QPen pen = QPen(Qt::red, 1, Qt::DashLine,
Qt::FlatCap, Qt::MiterJoin);
painter->setPen(pen);
painter->drawLine(opt.rect.left(), opt.rect.bottom()+1,
opt.rect.right(), opt.rect.bottom()+1);
painter->drawLine(opt.rect.right()+1, opt.rect.top(),
opt.rect.right()+1, opt.rect.bottom());

// draw rich text:
painter->save();
QTextDocument doc;
doc.setHtml( index.data().toString() );
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize( option.rect.size());
painter->setClipRect(option.rect);
painter->translate(option.rect.x(), option.rect.y());

//if (option.state & QStyle::State_Selected)
//painter->setBrush(option.palette.highlightedText());

doc.documentLayout()->draw(painter, context);
//doc.drawContents(painter, option.rect);
painter->restore();

// draw focus:
if (option.state & QStyle::State_Selected){
QPalette pal=option.palette;
QBrush brush = pal.highlight();
QColor col = brush.color();
col.setAlpha(127);
brush.setColor(col);
painter->fillRect(option.rect, brush);
//painter->fillRect(option.rect, option.palette.highlight());
}
}

huyhoangfool
1st November 2012, 09:34
To QTableWidget, I derive paint() function of QItemDelegate like this:


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

_ Draw rich text with Qt::AlignCenter:


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

Regards.