2

我希望我的 AbstracttableModel 子类data()方法返回 html 即

PreText<b>Text</b>PostText

并且此文本必须以 html 格式显示 int 表: PreText Text PostText

我怎样才能做到这一点?

4

2 回答 2

6

您可以为将显示 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();
    }
};

然后将其分配给视图:

view->setItemDelegateForColumn(0, new HtmlDelegate(view));
于 2012-04-09T19:02:40.407 回答
0

alexisdm 答案工作正常,但也许使用 aQLabel比它更轻QTextDocument,它也很聪明:

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();
    }
};

此外,文本在行上垂直对齐,使用QTextDocument.

于 2021-09-17T11:15:42.710 回答