QHeaderView -- diagonal headers?
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-p...view-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.
Code:
{
Q_OBJECT
public:
DiagTableHeader
(QWidget* parent
= 0);
virtual ~DiagTableHeader() {};
int sectionSizeHint(int logicalIndex) const;
private:
virtual void paintSection
(QPainter* painter,
const QRect
& rect,
int logicalIndex
) const;
};
and
Code:
{
}
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 {
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);
opt.initFrom(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
Re: QHeaderView -- diagonal headers?
Code:
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.
Re: QHeaderView -- diagonal headers?
Quote:
Originally Posted by
wysota
Code:
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
Code:
opt.initFrom(this->viewport();
opt.text = widget->text();
opt.section = logicalIndex;
if (logicalIndex > 0)
and added
Code:
sect.translate(-rect.left(), rect.bottom()/2);
before the Section drawControl
and
Code:
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
Re: QHeaderView -- diagonal headers?
Honestly I have no idea what you are trying to obtain, so it's hard to help in any way.
1 Attachment(s)
Re: QHeaderView -- diagonal headers?
Ok.. here's what the "requirements" look like (see attachment)
Vycke
Re: QHeaderView -- diagonal headers?
In that case rotating the painter while drawing will certainly not be enough...
Re: QHeaderView -- diagonal headers?
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
Re: QHeaderView -- diagonal headers?
Everything is possible. It's just a matter of asking oneself if the result is worth the effort...