0

I have a span of cells (1 column, 5 rows) where I would like display text on a 90 degree angle. I know I'll need to resize the geometry, but for now I can't even get the text to show up. In the middle row, I'm doing this within my subclassed QItemDelegate::paint()

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

Basically I get nothing printed in this case. A few other questions lead me to code like this. Am I missing something?

kiss-o-matic
  • 1,111
  • 16
  • 32
  • this should help: http://stackoverflow.com/a/22634541/1387438 – Marek R Oct 08 '15 at 18:44
  • That looks like it's for the QHeaderView. I need to rotate text in the actual view - in the cells. I assumed I could do this in a reimplemented paint(). – kiss-o-matic Oct 08 '15 at 18:49

1 Answers1

0

Pattern is same as in link I posted in comment. This should look like more or less like this. I could messed up some sign or do some typo.

#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();
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Thanks for the example. Still getting a blank though. Might be worth noting I've subclassed QStyledItemDelegate, although I wouldn't think that would make a difference. Will mess around a bit. – kiss-o-matic Oct 08 '15 at 20:27
  • The update contains exact contents of cpp file I used to verify this solution. It works perfectly. `QTransform::fromTranslate` part can be removed and still it works. – Marek R Oct 09 '15 at 06:21
  • Ok "perfectly" is to strong word. There is a problem with cells borders and this can't be simply fixed. – Marek R Oct 09 '15 at 13:01
  • take a pick inside Qt code, it can be quite easily fixed: https://github.com/RSATom/Qt/blob/ea2befbe538b031972adc76b926f1e6b95b689de/qtbase/src/widgets/itemviews/qitemdelegate.cpp#L655 – Marek R Oct 09 '15 at 13:10
  • I'm not seeing any difference. The only discrepency I see (and I apologize as it's an error in the original post) is that I'm inheriting QStyledItemDelegate, not QItemDelegate. I wouldn't think that would make a difference here though. – kiss-o-matic Oct 09 '15 at 13:19
  • I will try this code with a vanilla QItemDelegate and a simplier model/view, etc. The one I'm using now is one I've subclassed and heavily modified. – kiss-o-matic Oct 09 '15 at 13:26