0

我有一个单元格(1 列,5 行),我想以 90 度角显示文本。我知道我需要调整几何图形的大小,但现在我什至无法显示文本。在中间行,我在我的子类 QItemDelegate::paint() 中执行此操作

QString data = "String";
painter->rotate( 90 );
painter->drawText( opt.rect, Qt::AlignLeft, data );

在这种情况下,基本上我什么都没有打印出来。其他一些问题使我编写了这样的代码。我错过了什么吗?

4

1 回答 1

0

模式与我在评论中发布的链接相同。这应该看起来或多或少像这样。我可能搞砸了一些标志或做一些错字。

#include "customitemdelegate.h"
#include <QPainter>

CustomItemDelegate::CustomItemDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}

void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem newOption(option);
    QTransform transform(QTransform::fromTranslate(-option.rect.center().x(),
                                                   -option.rect.center().y()));
    transform.rotate(90);
    painter->setTransform(transform);
    transform=transform.inverted();
    newOption.rect=transform.mapRect(newOption.rect);
    QItemDelegate::paint(painter, newOption, index);

    // restore state of painter
    painter->setTransform(transform);
}

QSize CustomItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QItemDelegate::sizeHint(option, index).transposed();
}
于 2015-10-08T19:56:59.937 回答