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

问题描述 投票:1回答:1

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

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

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

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

Column's width sufficient

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

Column too narrow

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

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

c++ qt5 qpainter qstyleditemdelegate
1个回答
0
投票

很意外,我设法解决了它;-)

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.