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.
screenshot.jpg

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:

Qt Code:
  1. class VerticalTextHeaderStyle : public QProxyStyle
  2. {
  3. public:
  4. VerticalTextHeaderStyle(QStyle *style,
  5. int fontHeight)
  6. : QProxyStyle(style)
  7. {
  8. m_halfFontHeight=fontHeight/2;
  9. }
  10. QSize sizeFromContents(ContentsType type, const QStyleOption * option, const QSize & contentsSize, const QWidget * widget = 0) const
  11. {
  12. //force a very small size for testing:
  13. return QSize(10,10);
  14. }
  15. void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const
  16. {
  17. if (element == QStyle::CE_HeaderLabel) {
  18. const QHeaderView *hv = qobject_cast<const QHeaderView *>(widget);
  19. //Other header elements (e.g.: vertical header): do nothing specific
  20. if (!hv || hv->orientation() != Qt::Horizontal)
  21. {
  22. return QProxyStyle::drawControl(element, option, painter, widget);
  23. }
  24. const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option);
  25. //Text that shall be rotated is marked by a leading space. Bad hack, actually, but I lack better ideas..
  26. if ((header->text.length()>0)&&(header->text.left(1)!=" "))
  27. {
  28. //Text without rotation
  29. painter->save();
  30. if (header->text.contains("\n")) //I allow up to one line break.
  31. {
  32. QString zeile1=header->text.left(header->text.indexOf("\n"));
  33. QString zeile2=header->text.right(header->text.length()-header->text.indexOf("\n")-1);
  34. painter->drawText(header->rect.left(),header->rect.bottom()-3*header->fontMetrics.height()/2,zeile1);
  35. painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,zeile2);
  36. } else {
  37. painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,header->text);
  38. }
  39. painter->restore();
  40. return;
  41. }
  42. painter->save();
  43. //vertical text has to be rotated
  44. if (header->text.contains("\n")) //again, I allow up to one line break.
  45. {
  46. QString zeile1=header->text.left(header->text.indexOf("\n"));
  47. QString zeile2=" "+header->text.right(header->text.length()-header->text.indexOf("\n")-1);
  48. painter->translate(header->rect.center().x()-m_halfFontHeight,header->rect.bottom());
  49. painter->rotate(-90);
  50. painter->drawText(0,0,zeile1);
  51. painter->drawText(0,2*m_halfFontHeight,zeile2);
  52. } else {
  53. painter->translate(header->rect.center().x()+m_halfFontHeight,header->rect.bottom());
  54. painter->rotate(-90);
  55. painter->drawText(0,0,header->text);
  56. }
  57. painter->restore();
  58. return;
  59. }
  60. return QProxyStyle::drawControl(element, option, painter, widget);
  61. }
  62. protected:
  63. int m_halfFontHeight;
  64. };
To copy to clipboard, switch view to plain text mode 

"tabelle" is object of a class that inherits rom QTableView and I set its style this way:
Qt Code:
  1. tabelle->setStyle(new VerticalTextHeaderStyle(tabelle->style(),tabelle->font().pixelSize()));
To copy to clipboard, switch view to plain text mode 

When I call
Qt Code:
  1. tabelle->resizeColumnsToContents();
To copy to clipboard, switch view to plain text mode 
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?