首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QItemDelegate:将文本旋转90度

QItemDelegate:将文本旋转90度
EN

Stack Overflow用户
提问于 2015-10-09 02:29:48
回答 1查看 461关注 0票数 0

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

代码语言:javascript
复制
QString data = "String";
painter->rotate( 90 );
painter->drawText( opt.rect, Qt::AlignLeft, data );

基本上,在这种情况下,我没有打印任何内容。其他一些问题将我引向这样的代码。我是不是遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2015-10-09 03:57:00

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

代码语言:javascript
复制
#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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33023363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档