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.
{
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;
};
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;
};
To copy to clipboard, switch view to plain text mode
and
{
}
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());
}
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());
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks