【问题标题】:Custom QStyledItemDelegate to draw text with multiple colors自定义 QStyledItemDelegate 以绘制多种颜色的文本
【发布时间】:2019-04-30 14:29:15
【问题描述】:

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

为此,我实现了一个带有自定义paint() 函数的QStyledItemDelegate,该函数最终调用了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();
}

只要列的宽度足够,就可以正确绘制:

但是一旦列变小,我就会得到一个凌乱的溢出:

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

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

【问题讨论】:

    标签: c++ qt5 qpainter qstyleditemdelegate


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      相关资源
      最近更新 更多