Results 1 to 2 of 2

Thread: SizeHint of a horizontal header with vertical text

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Question 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.
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sedi (7th March 2014)

Similar Threads

  1. Vertical label on table horizontal header
    By ipkiss in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2014, 20:03
  2. Replies: 1
    Last Post: 24th January 2014, 07:28
  3. Replies: 21
    Last Post: 13th August 2013, 13:38
  4. How to get vertical/horizontal zoom box?
    By knicewar in forum Qwt
    Replies: 3
    Last Post: 23rd March 2010, 05:09
  5. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 16:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.