PDA

View Full Version : SizeHint of a horizontal header with vertical text



sedi
7th March 2014, 20:59
Hi,
I need a QTableview that can display some header items with vertical text while others remain horizontal.

Therefore I've subclassed QProxyStyle according to this thread (http://www.qtcentre.org/threads/51924-Vertical-label-on-table-horizontal-header?highlight=vertical+header+text) and modified it for the above needs.

My problem is, that on resizeColumnsToContents a column with vertical header text will stay as wide as the header text would need if it had been written horizontally.
10119

I've tried to overwrite the style's sizeFromContents to change that behaviour, but unfortunately to no avail.
Here is the code, that I use:


class VerticalTextHeaderStyle : public QProxyStyle
{
public:
VerticalTextHeaderStyle(QStyle *style,
int fontHeight)
: QProxyStyle(style)
{
m_halfFontHeight=fontHeight/2;
}
QSize sizeFromContents(ContentsType type, const QStyleOption * option, const QSize & contentsSize, const QWidget * widget = 0) const
{
//force a very small size for testing:
return QSize(10,10);
}
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const
{
if (element == QStyle::CE_HeaderLabel) {
const QHeaderView *hv = qobject_cast<const QHeaderView *>(widget);
//Other header elements (e.g.: vertical header): do nothing specific
if (!hv || hv->orientation() != Qt::Horizontal)
{
return QProxyStyle::drawControl(element, option, painter, widget);
}
const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option);
//Text that shall be rotated is marked by a leading space. Bad hack, actually, but I lack better ideas..
if ((header->text.length()>0)&&(header->text.left(1)!=" "))
{
//Text without rotation
painter->save();
if (header->text.contains("\n")) //I allow up to one line break.
{
QString zeile1=header->text.left(header->text.indexOf("\n"));
QString zeile2=header->text.right(header->text.length()-header->text.indexOf("\n")-1);
painter->drawText(header->rect.left(),header->rect.bottom()-3*header->fontMetrics.height()/2,zeile1);
painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,zeile2);
} else {
painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,header->text);
}
painter->restore();
return;
}
painter->save();
//vertical text has to be rotated
if (header->text.contains("\n")) //again, I allow up to one line break.
{
QString zeile1=header->text.left(header->text.indexOf("\n"));
QString zeile2=" "+header->text.right(header->text.length()-header->text.indexOf("\n")-1);
painter->translate(header->rect.center().x()-m_halfFontHeight,header->rect.bottom());
painter->rotate(-90);
painter->drawText(0,0,zeile1);
painter->drawText(0,2*m_halfFontHeight,zeile2);
} else {
painter->translate(header->rect.center().x()+m_halfFontHeight,header->rect.bottom());
painter->rotate(-90);
painter->drawText(0,0,header->text);
}
painter->restore();
return;
}
return QProxyStyle::drawControl(element, option, painter, widget);
}
protected:
int m_halfFontHeight;
};


"tabelle" is object of a class that inherits rom QTableView and I set its style this way:


tabelle->setStyle(new VerticalTextHeaderStyle(tabelle->style(),tabelle->font().pixelSize()));


When I call
tabelle->resizeColumnsToContents();
the result is not taking into account that the need of width has decreased by rotating the text.

How can I change the sizeHint - or maybe do something totally different to inform resizeColumnsToContents() about the correct column width?

anda_skoa
7th March 2014, 23:07
My guess is that the headerData() method of the model returns the horizontal text's size hint.

Cheers,
_