QItemDelegate:将文字旋转90度

时间:2015-10-08 18:29:48

标签: qt rotation qitemdelegate

我有一个单元格范围(1列,5行),我希望以90度角显示文本。我知道我需要调整几何体的大小,但是现在我甚至无法显示文本。在中间行,我在我的子类QItemDelegate :: paint()

中执行此操作
QString data = "String";
painter->rotate( 90 );
painter->drawText( opt.rect, Qt::AlignLeft, data );

基本上我在这种情况下没有打印任何东西。其他一些问题让我想到这样的代码。我错过了什么吗?

1 个答案:

答案 0 :(得分: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();
}