2

I want my AbstracttableModel subclass data() method to return html i.e.

PreText<b>Text</b>PostText

And this text must be displayed int table as in html: PreTextTextPostText

How can I do this?

zabulus
  • 2,373
  • 3
  • 15
  • 27

2 Answers2

6

You can create a delegate for the view that will display the html.

class HtmlDelegate : public QItemDelegate {
public:
    HtmlDelegate(QObject *parent = 0) : QItemDelegate(parent) {}

    // This function is only called to paint the text
    void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option,
                     const QRect &rect, const QString &text) const
    {
        QTextDocument doc;

        // Since the QTextDocument will do all the rendering, the color,
        // and the font have to be put back inside the doc
        QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
                                  ? QPalette::Normal : QPalette::Disabled;
        if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
            cg = QPalette::Inactive;
        QColor textColor = option.palette.color(cg, QPalette::Text);
        doc.setDefaultStyleSheet(QString("body { color: %1}")
                                 .arg(textColor.name()));
        doc.setDefaultFont(option.font);
        doc.setHtml(text);
        doc.setDocumentMargin(1); // the default is 4 which is too much

        painter->save();
        painter->translate(rect.topLeft());
        doc.drawContents(painter);
        painter->restore();
    }

    // bold and underlined characters take more space
    // so you have to redefine this function as well
    // (if you have a checkbox or an icon in the item, you will have
    // to include their size to the returned value)
    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
    {
        QTextDocument doc;
        doc.setDefaultFont(option.font);
        doc.setHtml(index.data(Qt::DisplayRole).toString());
        doc.setDocumentMargin(1);
        return doc.size().toSize();
    }
};

Then assign it to a view:

view->setItemDelegateForColumn(0, new HtmlDelegate(view));
alexisdm
  • 29,448
  • 6
  • 64
  • 99
0

alexisdm answer works fine, but maybe using a QLabel is lighter than QTextDocument and it works smartly too:

class HtmlDelegateWithLabel : public QItemDelegate 
{
public:
    HtmlDelegateWithLabel(QObject *parent = 0) : QItemDelegate(parent) 
    {
    }

    inline void setupLabel( QLabel& label, const QModelIndex &index ) const
    {
        QString txt = index.model()->data( index, Qt::DisplayRole ).toString();
        label.setText( txt );
    }

    // This function is only called to paint the text
    void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option,
                     const QRect &rect, const QString &text) const
    {
        QLabel label;
        setupLabel( label, option.index );
        label.setEnabled( option.state & QStyle::State_Enabled );
        label.setAttribute(Qt::WA_TranslucentBackground);
        
        painter->save();
        painter->translate(rect.topLeft());
        label.resize( rect.size() );
        label.render( painter );
        painter->restore();
    }

    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const
    {
        QLabel label;
        setupLabel( label, index );
        return label.sizeHint();
    }
};

Moreover, text gets aligned vertically on the row, which is not the case when using QTextDocument.

jpo38
  • 20,821
  • 10
  • 70
  • 151