1 Attachment(s)
SizeHint of a horizontal header with vertical text
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 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.
Attachment 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:
Code:
class VerticalTextHeaderStyle : public QProxyStyle
{
public:
VerticalTextHeaderStyle
(QStyle *style,
int fontHeight)
: QProxyStyle(style)
{
m_halfFontHeight=fontHeight/2;
}
{
//force a very small size for testing:
}
{
if (element
== QStyle::CE_HeaderLabel) { //Other header elements (e.g.: vertical header): do nothing specific
if (!hv || hv->orientation() != Qt::Horizontal)
{
return QProxyStyle::drawControl(element, option, painter, widget);
}
//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:
Code:
tabelle->setStyle(new VerticalTextHeaderStyle(tabelle->style(),tabelle->font().pixelSize()));
When I call
Code:
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?
Re: SizeHint of a horizontal header with vertical text
My guess is that the headerData() method of the model returns the horizontal text's size hint.
Cheers,
_