Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QItemDelegate:将文本旋转90度_Qt_Rotation_Qitemdelegate - Fatal编程技术网

Qt QItemDelegate:将文本旋转90度

Qt QItemDelegate:将文本旋转90度,qt,rotation,qitemdelegate,Qt,Rotation,Qitemdelegate,我有一个单元格跨度(1列,5行),在这里我希望以90度的角度显示文本。我知道我需要调整几何体的大小,但现在我甚至无法显示文本。在中间行中,我在我的子类QITEMCODATA::PrUnter()中这样做。 基本上我在这个箱子里什么也没印出来。还有几个问题让我想到了这样的代码。我遗漏了什么吗?模式与我在评论中发布的链接相同。这看起来应该差不多像这样。我可能会弄乱一些标志或者打字 #include "customitemdelegate.h" #include <QPainter> C

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

中这样做。
基本上我在这个箱子里什么也没印出来。还有几个问题让我想到了这样的代码。我遗漏了什么吗?

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

#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();
}
#包括“customitemdelegate.h”
#包括
CustomItemDelegate::CustomItemDelegate(QObject*parent)
:QItemDelegate(父级)
{
}
void CustomItemDelegate::paint(QPaint*painter,常量QStyleOptionViewItem&option,常量QModelIndex&index)常量
{
QStyleOptionViewItem新选项(选项);
QTransform transform(QTransform::fromtransplate(-option.rect.center().x(),
-option.rect.center().y());
变换。旋转(90);
画师->设置变换(变换);
transform=transform.inversed();
newOption.rect=transform.mapRect(newOption.rect);
QItemDelegate::paint(画师、新选项、索引);
//恢复状态
画师->设置变换(变换);
}
QSize CustomItemDelegate::sizeHint(常量QStyleOptionViewItem&option,常量QModelIndex&index)常量
{
返回QItemDelegate::sizeHint(选项,索引).transposed();
}

这应该会有帮助:这看起来是针对QHeaderView的。我需要在实际视图中旋转文本-在单元格中。我想我可以在重新实现的paint()中实现这一点。谢谢你的示例。但仍然是一片空白。可能值得注意的是,我已经将QStyledItemDelegate子类化了,尽管我认为这不会有什么不同。会有点乱。更新包含我用来验证此解决方案的cpp文件的确切内容。它工作得很好<代码>QTransform::fromTranslate部分可以删除,但仍然可以工作。确定“完美”是一个强大的词。单元格边框有一个问题,不能简单地解决。在Qt代码中选择一个选项,它可以很容易地解决:我看不出有什么不同。我看到的唯一不同之处(我很抱歉,因为这是原始帖子中的一个错误)是我继承的是QStyledItemDelegate,而不是QItemDelegate。但我不认为这会有什么不同。
#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();
}