Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. HTML rendering in QStyledItemDelegate sub-class
Forum Updated to NodeBB v4.3 + New Features

HTML rendering in QStyledItemDelegate sub-class

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    iharob
    wrote on last edited by
    #1

    I need html rendering in a custom item delegate. What i have already done

    Write it using QTextDocument, setHtml and QTextDocument::draw.

    Split QTextDocument into blocks using QTextDocument::begin() and draw each block using QTextBlock::charFormat() to setup the QPainter.

    Why the dont work:

    Solution 1, it works, but since there is no text-overflow css property, I can't use ellipsis for long lines.

    Solution 2, it also works, and i can manually use QFontMetrics::elidedText() so this solution is better but, the QTextCharFormat has wrong font information "althought the foreground and background colors are correct", for instance I used the <b></b> tag, so it should be bold but it isn't .

    Here is relevant code for the second solution, thank you for your help in advance.

    @static void
    textDocumentDrawContents(QPainter *painter, const QTextDocument *document,
    int width, bool selected, const QPalette &palette)
    {
    QTextBlock it;
    QAbstractTextDocumentLayout *layout;

    if ((painter == NULL) || (document == NULL))
        return;
    
    layout = document->documentLayout();
    if (layout == NULL)
        return;
    
    QVector<QTextFormat> formats(document->allFormats());
    
    for (it = document->begin() ; it != document->end() ; it = it.next())
    {
        int           index;
        const QRectF &rectangle(layout->blockBoundingRect(it));
    
        index = it.charFormatIndex();
        if ((index < 0) || (index >= formats.count()))
            continue;
    
        QTextFormat textformat(formats.at(index));
        if (textformat.isCharFormat() == false)
            continue;
    
        QString         text;
        QTextCharFormat format(textformat.toCharFormat());
        const QBrush   &foreground(format.foreground());
        const QFont    &font(format.font());
        QFontMetrics    metrics(font);
    
        text = metrics.elidedText(it.text(), Qt::ElideRight, width - 8);
    
        painter->setFont(font);
        if (selected == true)
            painter->setPen(palette.color(QPalette::HighlightedText));
        else
            painter->setPen(foreground.color());
    
        painter->drawText(rectangle, text);
    }
    

    }

    void
    renderHtml(QPainter *painter, const QStyleOptionViewItem &option, QTextDocument *document)
    {
    const QRect &frame(option.rect);
    bool selected;
    bool active;

    if (painter == NULL)
        return;
    
    selected  = ((option.state & QStyle::State_Selected) == QStyle::State_Selected);
    active    = ((option.state & QStyle::State_Active) == QStyle::State_Active);
    selected  = ((selected == true) && (active == true));
    
    painter->save();
    painter->translate(frame.left(), frame.top()); 
    
    textDocumentDrawContents(painter, document, frame.width(), selected, option.palette);
    
    painter->restore();
    

    }@

    1 Reply Last reply
    0
    • I Offline
      I Offline
      iharob
      wrote on last edited by
      #2

      Oh by the way. I am aware of QWebView, but I want a QWebViewLESS solution.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved