B
B
becks2013-12-06 00:14:22
Qt
becks, 2013-12-06 00:14:22

Qt Html Delegate for QTableView?

It is necessary to show html text in the cells of the QTableView table.
Main requirements:
1) The text is scaled to the size of the cell, and not placed all on one line.
2) There will be links in the text, a reaction on clicking on the link is needed - launch some slot.
3) You can select individual parts of the text for further copying somewhere. Editing/editing of an element is basically not needed.
Based on the second point, the delegate decided to build on the basis of QTextBrowser, since it supports the reaction to clicking on the link - anchorClicked.
Further there is a dilemma to be inherited from QItemDelegate or from QStyledItemDelegate. The first option is simpler because standard widgets are supported.
I tried both the first and the second, nothing happens, I killed a lot of time. I'll post option #2 here (inherit from QStyledItemDelegate). If anyone needs it, I can lay out the first option.
HtmlDelegate.h

#pragma once
#include <QStyledItemDelegate>
class HtmlDelegate : public QStyledItemDelegate
{
public:
    HtmlDelegate(QObject *parent=0) : QStyledItemDelegate(parent)
    {}
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

HtmlDelegate.cpp
#include <QPainter>
#include <QTextDocument>
#include "HTMLDelegate.h"
void HtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);
    painter->save();
    QTextBrowser browser;
    QFont font = browser.document()->defaultFont();
    font.setPixelSize(18);
    browser.document()->setDefaultFont(font);
    browser.setHtml(options.text);
    options.text = index.model()->data(index, Qt::DisplayRole).toString();
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
    painter->translate(options.rect.left(), options.rect.top());
    QRect clip(0, 0, options.rect.width(), options.rect.height());
    browser.document()->drawContents(painter, clip);
    painter->restore();
}
QSize HtmlDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);
    QTextBrowser browser;
    browser.setHtml(options.text);
    browser.document()->setTextWidth(options.rect.width());
    return QSize(doc.idealWidth(), browser.document()->size().height());
}

I checked it on QTableWidget, set it for the QTextBrowser column, everything is displayed and works fine, i.e. I have problems.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tugo, 2013-12-07
@tugo

I made a variant as far as I understood your task.
1. In the delegate, I show QLabel as an editor.
1a. You can wrap according to wordWrap.
2. I set up the view to permanently show the editor
3. In QLable I make a hyperlink
4. I set up for the signal void QLabel::linkActivated(const QString & link). I did not make this item, it seems to work.
Here is a terrible example , the meaning will be clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question