2

data()AbstracttableModelサブクラスメソッドが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の回答は問題なく機能しますが、おそらくaを使用するQLabel方が軽量で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 に答える