自定义 QStyledItemDelegate 绘制多种颜色的文本

Custom QStyledItemDelegate to draw text with multiple colors

我想在 QTableWidget 中显示两列,显示两个刺之间的差异(之前由一些 Levenshtein 距离算法计算)。零件存储在每个QTableWidgetItem的数据里面,作为一个QStringList。第一部分必须显示黑色,下一个显示红色,然后再次交替显示黑色、红色等。

为此,我实现了一个 QStyledItemDelegate 和一个最终调用 drawText() 方法的自定义 paint() 函数:

void DifferencesDelegate::drawText(QPainter *painter,
                                   const QStyleOptionViewItem &option,
                                   const QModelIndex &index) const
{
    painter->save();

    const QPen defaultPen = painter->pen();

    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);
    opt.text.clear();

    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);

    opt.rect.moveRight(opt.rect.right() + 3);

    int color = 1;
    for (const QString &part : index.data(Qt::UserRole).toStringList()) {
        color++;
        color = color % 2;
        if (color) {
            painter->setPen(Qt::red);
        } else {
            painter->setPen(defaultPen);
        }

        style->drawItemText(painter, opt.rect, opt.displayAlignment, opt.palette, true, part);
        opt.rect.moveRight(opt.rect.right() + painter->fontMetrics().width(part));
    }

    painter->restore();
}

只要列的宽度足够,这就会导致正确的绘制:

但是一旦列变小,我就会出现混乱的溢出:

这肯定是由于 opt.rect 应用于显示的每个部分,而不是整个文本。

唯一的问题是我不知道如何解决这个问题;-)任何帮助将不胜感激!提前致谢!

出乎意料的是,我设法解决了它;-)

void DifferencesDelegate::drawText(QPainter *painter, const QStyleOptionViewItem &option,
                                const QModelIndex &index) const
{
    if (! index.data(Qt::UserRole).isValid()) {
        TableDelegate::drawText(painter, option, index);
        return;
    }

    painter->save();

    const QPen defaultPen = painter->pen();

    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);
    opt.text.clear();

    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);

    constexpr int offset = 3;
    const QString ellipsis = QStringLiteral("…");
    const int ellipsisWidth = painter->fontMetrics().width(ellipsis);
    const int rightBorder = opt.rect.left() + opt.rect.width() - offset;

    QRect drawRect;
    int color = 1;
    int partWidth;
    bool overflow = false;

    opt.rect.moveRight(opt.rect.right() + offset);

    const QStringList parts = index.data(Qt::UserRole).toStringList();
    const int partsCount = parts.count();
    for (int i = 0; i < partsCount; i++) {
        color++;
        color = color % 2;
        if (color) {
            painter->setPen(Qt::red);
        } else {
            painter->setPen(defaultPen);
        }

        partWidth = painter->fontMetrics().width(parts.at(i));

        drawRect = opt.rect;
        if (drawRect.left() + partWidth + (i == partsCount - 1 ? 0 : ellipsisWidth) > rightBorder) {
            drawRect.setWidth(rightBorder - drawRect.left() - ellipsisWidth);
            overflow = true;
        }

        style->drawItemText(painter, drawRect, opt.displayAlignment, opt.palette, true,
                            parts.at(i));

        if (overflow) {
            drawRect.setLeft(rightBorder - ellipsisWidth);
            drawRect.setWidth(ellipsisWidth);
            style->drawItemText(painter, drawRect, opt.displayAlignment, opt.palette, true,
                                ellipsis);
            break;
        }

        opt.rect.moveRight(opt.rect.right() + partWidth);
    }

    painter->restore();
}