用HTML换行? QTableView和代理 [英] Word Wrap with HTML? QTableView and Delegates

查看:347
本文介绍了用HTML换行? QTableView和代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了

现在这是一个转折,我无法弄清楚这一点

Now here is a twist and I cant figure this out

如何进行html自动换行。例如,如果文本是:

How can I make my html word wrap. For instance if the text is :


我是现代少将的楷模,我已经知道
蔬菜动物和矿物,我知道英格兰的国王,而且我引用
从马拉松到滑铁卢的历史性战斗,以便
绝对...

"I am the very model of a modern major general, I've information vegetable animal and mineral, I know the kinges of England and I quote the fights historical from Marathon to Waterloo in order categorical..."

当前,所有内容都出现在tableView单元格的一行上。有没有办法让我自动换行?
我有以下绘画方法

Currently everything appears on one line on the cell of the tableView. Is there a way for me to word wrap this ? I have the following paint method

void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);

    painter->save();

    QTextDocument doc;
    doc.setHtml(options.text);

    options.text = "";
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);

    painter->translate(options.rect.left(), options.rect.top()+0);


    QRect clip(0, 0, options.rect.width(), options.rect.height());
    doc.drawContents(painter, clip);

    painter->restore();
}


推荐答案

请注意,性能会

void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);

    painter->save();

    QTextDocument doc;
    QTextOption textOption(doc.defaultTextOption());
    textOption.setWrapMode(QTextOption::WordWrap);
    doc.setDefaultTextOption(textOption);

    doc.setHtml(options.text);
    doc.setTextWidth(options.rect.width());

    options.text = "";
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);

    painter->translate(options.rect.left(), options.rect.top()+0);


    QRect clip(0, 0, options.rect.width(), options.rect.height());
    doc.drawContents(painter, clip);

    painter->restore();
}

这篇关于用HTML换行? QTableView和代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆