第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使項(xiàng)目視圖在Qt中呈現(xiàn)富文本(html)

如何使項(xiàng)目視圖在Qt中呈現(xiàn)富文本(html)

假設(shè)我的模型中包含以下項(xiàng)目的Qt :: DisplayRole字符串<span>blah-blah <b>some text</b> other blah</span>我想要QTreeView(實(shí)際上是任何項(xiàng)目視圖)將其呈現(xiàn)為富文本格式。而是,項(xiàng)目視圖默認(rèn)情況下將其呈現(xiàn)為純文本。如何實(shí)現(xiàn)所需的渲染?實(shí)際上,這是一個(gè)搜索結(jié)果模型。用戶輸入文本,針對(duì)該文本搜索某些文檔,然后向用戶顯示搜索結(jié)果,其中要搜索的單詞應(yīng)比周圍的文本大膽。
查看完整描述

3 回答

?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊

我的答案主要是受@serge_gubenko的啟發(fā)。但是,做了一些改進(jìn),使代碼最終在我的應(yīng)用程序中有用。


class HtmlDelegate : public QStyledItemDelegate

{

protected:

    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

    QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;

};


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

{

    QStyleOptionViewItemV4 optionV4 = option;

    initStyleOption(&optionV4, index);


    QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();


    QTextDocument doc;

    doc.setHtml(optionV4.text);


    /// Painting item without text

    optionV4.text = QString();

    style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);


    QAbstractTextDocumentLayout::PaintContext ctx;


    // Highlighting text if item is selected

    if (optionV4.state & QStyle::State_Selected)

        ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));


    QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);

    painter->save();

    painter->translate(textRect.topLeft());

    painter->setClipRect(textRect.translated(-textRect.topLeft()));

    doc.documentLayout()->draw(painter, ctx);

    painter->restore();

}


QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const

{

    QStyleOptionViewItemV4 optionV4 = option;

    initStyleOption(&optionV4, index);


    QTextDocument doc;

    doc.setHtml(optionV4.text);

    doc.setTextWidth(optionV4.rect.width());

    return QSize(doc.idealWidth(), doc.size().height());

}


查看完整回答
反對(duì) 回復(fù) 2019-10-17
  • 3 回答
  • 0 關(guān)注
  • 1577 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)