PDA

View Full Version : QHeaderView -- diagonal headers?



vycke
23rd April 2008, 17:32
My project has a requirement to have diagonal headers on a few tables, allowing long labels to not take up more horizontal space. I found http://www.qtcentre.org/forum/f-qt-programming-2/t-subclassing-qheaderview-2282.html
but when I subclass the QHeaderView & reimplement paintSection I have only the first label diagonal -- the 2nd label is missing. And the section pieces are not rotated to match.



class DiagTableHeader : public QHeaderView
{
Q_OBJECT
public:
DiagTableHeader(QWidget* parent = 0);
virtual ~DiagTableHeader() {};

int sectionSizeHint(int logicalIndex) const;
QSize sizeHint() const;

private:
virtual void paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const;
};


and



DiagTableHeader::DiagTableHeader(QWidget* parent) : QHeaderView(Qt::Horizontal, parent)
{
}

int DiagTableHeader::sectionSizeHint(int logicalIndex) const
{
QSize size = ((QTableWidget*)parentWidget())->horizontalHeaderItem(logicalIndex)->sizeHint();
int diag = sqrt(pow(size.height(), 2) + pow(size.width(), 2));
return diag;
}

QSize DiagTableHeader::sizeHint() const
{
QSize size = QHeaderView::sizeHint();
int diag = sqrt(pow(size.height(), 2) + pow(size.width(), 2));
size.setHeight(diag); // leave the width the same right now
return size;
}

void DiagTableHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
{
painter->rotate(-45);
QTableWidgetItem* widget = ((QTableWidget*)parentWidget())->horizontalHeaderItem(logicalIndex);
QStyleOption opt;
opt.initFrom(this->viewport());
QStylePainter sect(this->viewport());
sect.rotate(-45);
sect.drawControl(QStyle::CE_HeaderSection, opt); // this doesn't seem to do anything
painter->drawText(rect.left(), rect.bottom(), widget->text());
}


Note that the 1 label that does show is shifted to the right (even though rect.left() = 0) and up .

Is there something obvious that I'm missing? Is this even possible?

Thanks for whatever help you can give me.

Vycke

wysota
23rd April 2008, 17:37
sect.drawControl(QStyle::CE_HeaderSection, opt)
This won't do anything as your opt object doesn't contain any useful information about the section. Initalizing the object is not enough, you have to fill it with data.

vycke
23rd April 2008, 19:32
sect.drawControl(QStyle::CE_HeaderSection, opt)
This won't do anything as your opt object doesn't contain any useful information about the section. Initalizing the object is not enough, you have to fill it with data.

Ok... Changed that opt to


QStyleOptionHeader opt;
opt.initFrom(this->viewport();
opt.text = widget->text();
opt.section = logicalIndex;
if (logicalIndex > 0)
opt.position = QStyleOptionHeader::Middle;


and added

sect.translate(-rect.left(), rect.bottom()/2);
before the Section drawControl
and


sect.translate(-rect.left(), rect.bottom()/2);
sect.drawControl(QStyle::CE_HeaderLabel, opt);
after the Section draw and removed the drawText().

The translates help place the controls where they belong... but it appears that the whole header is rotated, not just the sections inside it.

Any further clues? Or is what I'm trying actually impossible?

Vycke

wysota
23rd April 2008, 20:28
Honestly I have no idea what you are trying to obtain, so it's hard to help in any way.

vycke
28th April 2008, 19:49
Ok.. here's what the "requirements" look like (see attachment)

Vycke

wysota
28th April 2008, 20:28
In that case rotating the painter while drawing will certainly not be enough...

vycke
28th April 2008, 23:12
Yeah :) Kinda figured that out. The real question, I guess, is "IS it possible?" I might just tell the guys doing the req's that they get horizontal text or vertical text, but that diagonal stuff is "right out" :)

Vycke

wysota
29th April 2008, 00:54
Everything is possible. It's just a matter of asking oneself if the result is worth the effort...