Hi,
have used the code of marcel creating a qtrewidget with word-wrap function and it works nice. The next step should be display html text instead of plain text like now. I have used the following code in overridden paint():
painter->save();
doc.setHtml(index.data().toString());
doc.setPageSize(option.rect.size());
painter->translate(option.rect.x(), option.rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
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();
To copy to clipboard, switch view to plain text mode
( instead of painter->drawText() ) but with no luck: the treeWidget is illegible.
Any hint?
Some code:
delegate.cpp
{
painter->save();
doc.setHtml(index.data().toString());
doc.setPageSize(option.rect.size());
painter->translate(option.rect.x(), option.rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
// painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap, index.data().toString());
}
QSize delegate
::sizeHint(const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
float rw = float(p->viewport()->size().width());
float tw = fm.width(index.data().toString());
float ratio = tw/rw;
int lines = int(ratio) + 1;
return QSize(rw,lines
*fm.
height());
}
void delegate::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();
// painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap, index.data().toString());
}
QSize delegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QTreeWidget *p = (QTreeWidget*)parent();
QFontMetrics fm(p->font());
float rw = float(p->viewport()->size().width());
float tw = fm.width(index.data().toString());
float ratio = tw/rw;
int lines = int(ratio) + 1;
return QSize(rw,lines*fm.height());
}
To copy to clipboard, switch view to plain text mode
Thanks
Bookmarks